Write a program FileCounter.java that reads a text file text1.txt and reports the number of characters and lines contained in the file. For example, the output should be as follows:
Lines: 5 Chars: 124
Note: Write a main() method to test in the FileCounter.java. The file path should use relative path. Must use the provided text file text1.txt.
The text file say
This is an example 1
This is an example 1 2.
This is an example 1 2 3.
This is an example 1 2 3 4.
This is an example 1 2 3 4 5.
2. Suppose you have a binary file geo.dat (see provided data file) of data for a geological survey, such that each record consists of a longitude, a latitude, and an amount of rainfall, all represented by double. Write a program GeologicalData.java to read this file’s data and print them on the screen, one record per line.
Note: Write a main() method to test the code in the GeologicalData.java. The file path should use relative path and the actually file path should be passed into the method getData(String fileName) in the main() method. Must use the provided file geo.dat which has already contained the following data.
-143.78914479979002 59.482245058443084 17.645846630300042 -117.80303714838257 -25.94559103704657 4.513879725440151 49.63942128783603 129.1664680590585 6.825838082783708 -79.66606860669573 -18.800024954102696 9.501515919083086 62.82522223390336 -60.409595996464006 2.6984128008196984
The partial Coding is
public class FileCounter {
}
The other Coding
public class GeologicalData {
public static void getData(String fileName){
}
}
In: Computer Science
PLEASE COMPLETE BOTH PARTS (LANGUAGE: C++ if necessary)
Part I
For the following example, determine the number of comparisons that will take place during a search using both searching algorithms (Linear search and binary search). Also, list each comparison for each algorithm(Linear and binary). The number being searched for is 13.
List = {1,3,5,9,10,11,15,17,19,23,24,25,29,31,33,37,38,39,43,45,47,51,52,53,57,59,61}
Part 2
For the following example, determine the number of swaps that will take place during a sort using both sorting algorithms (bubble and selection sort). Also, list each swap for each algorithm (bubble and selection)
List = {23, 10, 3, -5, 2, 7, 0, 20, 15, 17, 9, 11, 4, 1}
In: Computer Science
Write a function called evenUp that returns the result of increasing the first even digit in a positive integer parameter by 1. (Your solution should use no more than 10 lines of code. Your function can return any convenient value of your choice if the parameter is not positive.) For example, a program that uses the function evenUp follows.
public class P5 {
public static void main(String args[]) {
System.out.println(evenUp(1232)); // prints 1332 only the first even 2 changes
System.out.println(evenUp(1332)); // prints 1333
System.out.println(evenUp(1333)); // prints 1333 no even digit to change
System.out.println(evenUp(22)); // prints 32 System.out.println(evenUp(2)); // prints 3
In: Computer Science
Through Python, a dictionary or list is often used to store a sequential collection of elements. Suppose, for instance, that you need to read 50 numbers, compute their average, and then compare each number with the average to determine whether it is below or above the average. In order to do this (without the use of list), you would first declare 50 variables to store the 50 numbers. This means you would have to create 50 variables and repeatedly write almost the identical code 50 times. Writing a program this way is impractical. To overcome this issue, you can make use of a list to store all the 50 numbers and access them through a single list variable.
For your initial post, describe the difference between dictionary and list in Python and explain when each would be used. Provide an example of how each dictionary and list would be implemented within Python. 250 WORDS
In: Computer Science
Develop a Java application to implement a binary tree data structure. A tree data structure starts from the top node, called the root. Each node in the tree has a set of children, which are also nodes of the tree and can have children of their own, and so on. This keeps on going till we get to the bottom of the tree, to the “leaf” nodes. Each node in the tree, except for the root, has a parent. A binary tree is a specialized form of a tree data structure in which each node in the tree has at most two children.
A binary tree can be represented using the format in the example below. Each line contains at most three characters. The first character is the ID of the node itself. The next two characters are the child nodes. If a line has only 2 characters, then that node has only one child. If a line has one character only, then it is a leaf node (i.e., it has no children). Example
A B C
B D E
C F G
D H I
E J K
F L
G
H
I
J
K
L
The application should exhibit the following functionality (see the sample output provided):
o If the user wants to add a node, request for the name / ID of the new node to be added as well as the name of the desired parent of that node.
▪ If the parent node already has two children, the new node cannot be added since this is a binary tree (and each node can only have at most two children).
▪If the parent node already has one child, use recursion to add the new node as the second child (right child) of the parent node.
▪ If the parent node has no children, use recursion to add the new node as the left child
of the parent node.
o If the user wants to know the size of the tree (i.e., the number of nodes in the tree or sub-tree), request for the name / ID of the node that is the root of the desired tree / sub-tree. The size of a tree or sub-tree is the number of its children and other ‘descendants’ (including any leaf nodes) plus one – the root node itself. For example, the size of the sub-tree with root ‘B’ in Figure 2 above is 7.
▪ Recursively count the number of nodes in the tree / sub-tree and display this with an appropriate message.
o If the user wants to find a node, request for the name / ID of the node to search for in the tree.
▪ Search recursively for the node in the tree; if the node is found, display an appropriate
message, otherwise, indicate that the node does not exist in the tree.
•Display the menu options.
o If the user wants to exit, terminate the program.
A sample output (to be followed exactly) is included below.
Example Output
ABC
BDE
DHI
H
I
EJK
J
K
CFG
FL
L
G
There are 12 nodes in this tree.
Please select from one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->1
Please input the node you want to add->
P
Please input the parent node of P->
A
Parent already has 2 children.
Please select from one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->1
Please input the node you want to add->
P
Please input the parent node of P->
F
Node successfully added!
ABC
BDE
DHI
H
I
EJK
J
K
CFG
FLP
L
P
G
Please select from one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->2
Please input the root node->
F
There are 3 nodes in that tree
FLP
Please select from one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->3
Please input the node you want to look for->
P
Node P found!
P
Please select from one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->3
Please input the node you want to look for->
N
Node N does not exist.
Please select from one of the following options: 1. Add Node
2. Tree Size
3. Find Node
0. Exit
->0
The main class in your application should be named BinaryTree. This class should instantiate an instance of the second class – to be named TreeDataStructure – and that instance should be used to call the appropriate methods to generate the initial tree, display the menu to the user and exhibit the functionality described above. Apart from the main method, the BinaryTree class should also have a method to print the menu options (void printMenu() ). You can copy and paste the code below into your main class to generate the initial tree.
public static void main(String[] args) {
TreeDataStructure root =
root.addChild("B", "A");
root.addChild("C", "A");
root.addChild("D", "B");
root.addChild("E", "B");
new TreeDataStructure("A");
root.addChild("F", "C");
root.addChild("G", "C");
root.addChild("H", "D");
root.addChild("I", "D");
root.addChild("J", "E");
root.addChild("K", "E");
root.addChild("L", "F");
The TreeDataStructure class should implement the INode interface (which is provided below). The methods addChild(), find(), printTree(), and size() in the TreeDataStructure class must be implemented recursively. NOTE: Using recursion is one of the main objectives of this assignment. A solution that does not use recursion to implement the required functionality will earn zero points. Create an interface named INode within your package (following steps similar to how you would create a class) and copy and paste the interface code below into it.
public interface INode {
public boolean addChild(String ID, String parentID);
public INode find(String value);
public INode getParent();
public int size();
public String toString();
public String getId();
public void printTree();
Hints
In: Computer Science
Dynamic Array
To save computer memory, we need to use dynamic array. In the first
version of our stack implementation, the instance variable has a
fixed capacity, which is 128. There will be an error when the stack
is bigger than 128. For bigger data, you increased the array size.
But that very long array could be wasted for smaller data.
In order to solve this dilemma, we start with a small array, and
increase the capacity only when it is needed. You will modify the
push operation by resizing the array when it overflows. And you
also need to implement the resize(...) method. The details of the
code are in the slides. Submit your dynamic stack in the submission
site here. Your can use the doubling strategy in resizing.
import java.io.*;
import java.util.*;
public class Stack2540ArrayDynamic {
int CAPACITY = 128;
int top;
String[] stack;
public Stack2540ArrayDynamic() {
stack = new String[CAPACITY];
top = -1;
}
public int size() {
return top + 1; }
public boolean isEmpty() {
return (top == -1); }
public String top() {
if (top == -1)
return
null;
return stack[top];
}
// add resize method
// add pop method
public void push(String element) {
// add something here
top++;
stack[top] = element;
}
}
* Please complete the resize, pop, and push methods.
In: Computer Science
PYTHON PROGRAMMING
The colors red, blue, and yellow are known as the primary colors because they cannot be made by mixing other colors. When you mix two primary colors, you get a secondary color, as shown here:
For this program, you will employ list data structures to store a group of objects by designing a program that prompts the user to enter the names of two primary colors to mix. If the user enters anything other than “red,” “blue,” or “yellow,” the program should display an error message. Otherwise, the program should display the name of the secondary color that results.
Once you have completed the program, take a screen shot of the completed functionality (including the input and the output).
Also Describe your process in developing the program in detail.
In: Computer Science
II. Application 1 and 2 run concurrently. Whenever a timeout interrupt occurs, the kernel switches control between the applications. Show the order of instruction execution, assuming application 1 is currently running. (42 pts)
Application 1 |
Application 2 |
... instruction i (timeout interrupt) instruction i+1 ... instruction k (timeout interrupt) instruction k+1 ... |
instruction 0 ... instruction j (timeout interrupt) instruction j+1 ... |
_______
_______
_______
_______
_______
_______
In: Computer Science
Java code: Use Radix sort to sort them in alphabetic order. How many passes would be made through the outer while loop? Trace the contents of the list after each of these passes.
Define the efficiency of the following code fragment:
K := 1
repeat
J := 1
repeat
……..
J := J * 2
until J >= N
K := K + 1
until K >= N
In: Computer Science
java
8.3: Histogram
Design and implement an application that creates a histogram that
allows you to visually inspect the frequency distribution of a set
of values. The program should read in an arbitrary number of
integers that are in the range 1 to 100 inclusive; then produce a
chart similar to the one below that indicates how many input values
fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk
for each value entered.
1 - 10 | *****
11 - 20 | ****
21 - 30 | *********
31 - 40 | **
41 - 50 | **************
51 - 60 | ******
61 - 70 | ****
71 - 80 | ********
81 - 90 | *
91 - 100 | ***
In: Computer Science
c++
Programming Assignment 1: Game of Life
The objective of this programming assignment is to design and implement what is known as the “Game of Life”, conceptualized by the British mathematician John Horton Conway in 1970 to simulate the evolution patterns in a population of living organisms.
The game board is seeded with an initial population pattern, and then evolves based on the set of rules defining when a cell dies or is born into life. A cell’s life cycle depends on the state of its neighboring cells.
The game has no players - a user interacts with the “Game of Life” by creating an initial configuration and then observes how it unfolds. Some already known initial patterns have interesting predictable properties – you might discover a new one!
The implementation will only utilize the C++ programming concepts covered by pre-requisite courses. We will take an iterative approach; walk through the phases of specification, design and implementation.
Task #1 - Preparation
Please familiarize yourself with the topic by watching the following video:
https://www.youtube.com/watch?v=CgOcEZinQ2I (Links to an external site.)
Task #2 - Specification
We will discuss the game specification in class. What functionality (encapsulated in the member functions/methods) will be needed to accomplish the goal? Which of those methods will be publicly available (API) to invoke and operate the game, and which methods will be private to carry out the internal implementation details?
The specification will then be documented as part of the GameOfLife.h file – the comment section of the file, including the Pre-Conditions and Post-Conditions, followed by methods’ prototypes.
A skeleton .h file will be provided.
Task #3 - Object Oriented Design
Object Oriented design asks to model all significant entities in a problem domain as classes, with appropriate structural relationships between them. We would then have two classes - one corresponding to the game itself (example: GameOfLife_T), and another one corresponding to a cell (example: Cell_T). The GameOfLife class will contain a private structure(s) of Cell_T. The entities will each have a set of responsibilities, reflected in the corresponding member functions/methods.
The GameOfLife_T would have to be able to:
Seed/initialize the game board with
a predefined pattern (from a file)
a randomly generated pattern
Run the game
Execute the rules for every cell in order to generate the next generation of our cell population
Display the next generation matrix
Stop the game (ask a user up front how many generations to display, or ask a user whether to continue every step of the way)
The Cell_T would have to be able to:
Know, if it is alive or dead, and communicate so
Die
Come alive
Task #4 - Implementation
There are many ways to implement the generation of the “next step/generation” game board, and it will be up to a student to decide. We will discuss the options in class.
The display of the board will be character based, simply accomplished with console output.
--------------------
-----*--------------
----*-*-------------
--------*-----------
Please put your game in Namespace CS
Deliverables
A zip/archive file named <CS_PA1_LastName_FirstName.zip> containing:
h header file
cxx implementation file
cxx with main() to instantiate and run the game
txt – configuration file to seed the initial population pattern
In: Computer Science
For a business operating in a competitive and evolving online market, it is MOST important for a security policy to focus on:
A. defining policies for new technologies.
B. enabling adoption of new technologies.
C. requiring accreditation for new technologies.
D. managing risks of new technologies.
Correct Answer: B???? or D?????? or Others…...
______________________
Note
■ Some good websites claim that the correct answer is D ("managing risks of new technologies").
■ Others good websites claim that the correct answer is B ("enabling adoption of new technologies").
■ Why D and not B? Why B and not D?
Many thanks!
In: Computer Science
I. Fill in the blank by matching the high level construct correct number to the corresponding letter of the instruction sequence. Write one sentence to support your answer.
1. Access the disk block i.
2. Enqueue x into Q.
3. C= AX B where A, B, C are matrices.
4. Send file f to printer P.
5. Load program p into memory and execute p.
Copy f into a temporary file f'.
Wait until printer P not busy.
Reserve printer P.
Send file f' to printer P.
Release printer P.
____
for all i:
for all j:
for all k:
Cij = Cij + Aik x Bkj
_____
Given a file block i, determine the track number t and the block number j within t that correspond to block i. Access block j on track t.
_____
If program p is too large to fit into memory, divide p into
smaller partitions p1, p2, ...
Load p1 and start execution.
Whenever a different partition pi is needed, save current partition
and load pi.
Resume execution.
_____
Allocate new queue element e.
Copy x into e.
Insert e at rear of Q.
____
In: Computer Science
Respond to the following in a minimum of 175 words:
Remember, decision structures are also called selection structures.
Describe decision processing control structures. Provide a unique scenario that requires the use of decision processing to solve a business problem.
In: Computer Science
Hi, I have this code so far and need to modify it so that the output does not print something like 2x^0 but instead will just print 2. Currently it prints 2x^0. I also am having a problem with the output of Polynomial 1 - Polynomial 2. If both coefficient values for the polynomials are equal instead of Polynomial 1 - Polynomial 2 = 0 it outputs nothing. Just Polynomial 1 - Polynomial 2 =
For example if I input (assuming there is only one coefficient for each polynomial)
Coefficient 1 for polynomial 1: 1 1
Coefficient 1 for polynomial 2: 1 1
You get no output instead of 0.
Could you please edit my code so that it shows this, or teach me in detail of how to change this. Thank you.
#include <cmath>
#include <iostream>
using namespace std;
const int DEFAULTPOLY = 10; //Default size of our dynamic coefficient array
//DO NOT modify the class header
class Poly
{
private:
//Data members
int arraySize; //size of array
int *coeff; //dynamic array
public:
Poly();
Poly(int size);
Poly(const Poly& aPoly);
~Poly();
const Poly& operator=(const Poly& aPolt);
void grow(int newSize);
int degree() const;
void setCoeff(int value, int i);
int getCoeff(int i) const;
void negate();
friend Poly operator+(const Poly& aPoly, const Poly& bPoly);
friend Poly operator-(const Poly& aPoly, const Poly& bPoly);
friend bool operator==(const Poly& aPoly, const Poly& bPoly);
friend ostream& operator<<(ostream& out, const Poly &aPoly);
};
//default constructor
Poly::Poly()
{
arraySize = DEFAULTPOLY;
coeff = new int[DEFAULTPOLY];
for(int i=0; i<DEFAULTPOLY; i++)
coeff[i] = 0;
}
//constructor
Poly::Poly(int size)
{
arraySize = size;
coeff = new int[size];
for(int i=0; i<size; i++)
coeff[i] = 0;
}
//copy constructor
Poly::Poly(const Poly& aPoly)
{
arraySize = aPoly.arraySize;
if(coeff){
delete [] coeff;}
coeff = new int[arraySize];
for(int i=0; i<arraySize; i++)
coeff[i] = aPoly.coeff[i];
}
//destructor
Poly::~Poly()
{
delete []coeff;
}
// = operator overloading
const Poly& Poly::operator=(const Poly& aPoly)
{
if(this == &aPoly){
return *this;}
if(coeff){
delete [] coeff;}
arraySize = aPoly.arraySize;
coeff = new int[arraySize];
for(int i=0; i<arraySize; ++i)
{
coeff[i] = aPoly.getCoeff(i);
}
return *this;
}
// function to grow the polynomial
void Poly::grow(int newSize)
{
if(arraySize>=newSize) return;
Poly temp(newSize);
for(int i=arraySize; i<newSize; i++)
temp.coeff[i] = 0;
for(int i=0; i<arraySize; i++)
temp.coeff[i] = coeff[i];
delete []coeff;
coeff = temp.coeff;
}
// function to return the degree of the polynomial
int Poly::degree() const
{
int i;
for(i=DEFAULTPOLY-1; coeff[i]==0; i--);
return i;
}
// function to set the coefficient of the polynomial
void Poly::setCoeff(int value, int i)
{
if(i>arraySize) grow(i);
coeff[i] = value;
}
// function to return coefficient of the polynomial
int Poly::getCoeff(int i) const
{
if(i>=arraySize) return 0;
return coeff[i];
}
// function to negate the polynomial
void Poly::negate()
{
for(int i=0; i<arraySize; i++)
coeff[i] = -coeff[i];
}
// + operator overloading
Poly operator+(const Poly& aPoly, const Poly& bPoly)
{
Poly temp;
int i;
for(i=0; i<DEFAULTPOLY-1; i++)
temp.coeff[i] = aPoly.coeff[i] + bPoly.coeff[i];
return temp;
}
// - operator overloading
Poly operator-(const Poly& aPoly, const Poly& bPoly)
{
Poly temp;
int i;
for(i=0; i<DEFAULTPOLY-1; i++)
temp.coeff[i] = aPoly.coeff[i] - bPoly.coeff[i];
return temp;
}
// == operator overloading
bool operator==(const Poly& aPoly, const Poly& bPoly)
{
int i;
for(i=0; i<DEFAULTPOLY; i++)
if(aPoly.coeff[i]!=bPoly.coeff[i]) return false;
return true;
}
// << operator overloading
ostream& operator<<(ostream& out, const Poly
&aPoly)
{
for(int i=aPoly.degree(); i>=0; i--)
{
if(aPoly.coeff[i]==0) continue;
if(i==aPoly.degree() || aPoly.coeff[i]<0)
out<<aPoly.coeff[i]<<"X^"<<i<<" ";
else
out<<"+ "<<aPoly.coeff[i]<<"X^"<<i<<"
";
}
out<<endl;
return out;
}
/////////////////////////////////////////////////////////////////////////////////////////
int main() {
int numCoeff, coeffValue, coeffDegree, x;
Poly poly1, poly2;
//prompt user for number of coefficients
cout << "How many coefficients for polynomial 1?" << endl;
cin >> numCoeff;
for(int i=0; i<numCoeff; ++i)
{
cout << "Coefficient " << i+1 << " for polynomial 1:";
cin >> coeffValue >> coeffDegree;
poly1.setCoeff(coeffValue, coeffDegree);
}
cout << endl << "How many coefficients for polynomial
2?" << endl;
cin >> numCoeff;
for(int i=0; i<numCoeff; ++i)
{
cout << "Coefficient " << i+1 << " for polynomial 2:";
cin >> coeffValue >> coeffDegree;
poly2.setCoeff(coeffValue, coeffDegree);
}
//sample test cases for degree and operator <<
cout << endl << "Polynomial 1 = " << poly1 << endl;
cout << "Polynomial 1 has degree " << poly1.degree() << endl;
cout << "Polynomial 2 = " << poly2 << endl;
cout << "Polynomial 2 has degree " << poly2.degree() << endl;
//Sample test cases for operator+ and operator-
cout << endl << "Polynomial 1 + Polynomial 2 = " << poly1 + poly2 << endl;
cout << "Polynomial 1 - Polynomial 2 = " << poly1 - poly2 << endl << endl;
//Sample test cases for operator ==
if(poly1 == poly2)
{
cout << "Two polynomials are the same." << endl;
}
else
{
cout << "Two polynomials are different." << endl;
}
return 0;
}
In: Computer Science