The following program supposes to calculate the area of a rectangle and square. Then, produce the following output.
Area of Rectangle:
50
Unfortunately, the program has compile-time and run-time errors that prevent the program from running and producing the correct result. Using table 3.1 below, allocate the error(s) on each program line.
1 public class RectangleArea {
2 Public static void main() {
3 int width == 10;
4 int height = 5;
5 int recArea = width + height;
6 System.Out.print(Area of
Rectangle:);
7 System.out.println(“recArea”)
8
9
Note:
Some of the answers are given to you as an example.
Table 3.1: compile-time / run-time error
|
Line No |
compile-time / run-time error |
|
1 |
No error |
|
2 |
Error.
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
In: Computer Science
Consider the following java class:
class Student {
private int student_Number;
private String student_Name;
public Student(int stNo,String name) {
student_Number=stNo;
student_Name=name;
}
public String getName() {
return student_Name;
}
public int getNumber() {
return student_Number;
}
public void setName(String st_name) {
student_Name = st_name;
}
}
Write a Tester class named StudentTester which contains the following instruction:
Note:
Include the screenshot of the program output as a part of your answer. Otherwise, you will be marked zero for this question.
In: Computer Science
In: Computer Science
What java program would you write to solve the following problems and why does it work? Please also comment on other students’ code at least three times. 1) Implement MyArrayList and MyLinkedList using MyList interface and MyAbstractList as defined in Java Collection Framework. 2) For the following problem use your own created MyArrayList or MyLinkedList if needed. Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters. Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. For the last line of text, it should be left justified and no extra space is inserted between words.
In: Computer Science
Write a complete program called EvenNums that uses a
method to create arrays of even numbers starting 1 of the given length:
public static int[] getEvenNums(int length) {}
EX:
int[] nums = getEvenNumbers(6);
System.out.println(Arrays.toString(nums));
expected output:
[2, 4, 6, 8, 10, 12]
In: Computer Science
Matlab
Theoretical Calculations:
t = (-.00025: .00025: 100000);
A1 = 29;
A2 = 1.2*A1;
t1 = (37.2/2)*.00025;
t2 = -(41.3/10)*.00025;
x1 = A1*co(2*pi*(4000)*(t-t1));
x2 = A2*co(2*pi*(4000)*(t-t2));
x3 = x1 + x2;
Remember that the phase of a sinusoid can be calculated after measuring the time location of a positive peak,3 if we know the frequency.
(a) Make measurements of the “time-location of a positive peak” and the amplitude from the plots of x1(t) and x2(t), and write those values for Ai and ti directly on the plots. Then calculate (by hand) the phases of the two signals, x1(t) and x2(t), by converting each time-shift ti to phase. Note: when doing computations, express phase angles in radians, not degrees!
(b) Measure the amplitude and time-shift of x3(t) directly from the plot and then calculate the phase φ3 by hand. In your report, show how the amplitude and time-shift were measured, and how the phase was calculated.
(c) Now use the phasor addition theorem. Carry out a phasor addition of complex amplitudes for x1(t) and x2(t) to determine the complex amplitude for x3(t). Use the complex amplitude for x3(t) to verify that your previous calculations of A3 and φ3 were correct.
In: Computer Science
In: Computer Science
Use c++language Use the pseudocode description below to write a program that uses an if, else if, else decision structure for a program that will determine if someone lives in Boston. 1. display message that describes what the program will do. 2. ask the user to input an answer to the question: Do you live in Boston?. 3. if they entered 'y', display a message confirming that they live in Boston. 4. if they entered 'n' , display a message confirming that they don't live in Boston. 5. if they entered an invalid entry, display a message telling them.
In: Computer Science
What is Industry 4.0 and how is it related to IoT?
In: Computer Science
Using MARS and MIPS
2. Write and test a program to swap the contents of two registers. Assume there is only one additional register available that may be destroyed.
Your program should initially read values (of type integer, float or string) into registers. Next, it will swap the values read, then print the final contents of each register
(you are not allowed to use "move").
In: Computer Science
The three major types of Backbone Networks are
based on the devices used. In practice, it is most common to
use a combination of these architectures. In your opinion
what
best practices do you recommend in the following?
(a) Architectures.
(b) Technologies.
(c) Implications for management
In: Computer Science
IN JAVA
Rational Numbers
Create a Rational number class in the same style as the Complex number class created in class. That is, implement the following methods:
You must also provide a Main class and main method to fully test your Rational number class.
In: Computer Science
Java code for a binary tree that does the following:
Display a menu to the user and request for the desired
option.
Based on the user’s input, request for additional information as
follows:
o If the user wants to add a node, request for the name (or 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).
Display an appropriate message to the user.
Display the menu again for the user to make another choice.
If the parent node already has one child, add the new node as the
second child (right
child) of the parent node. Use recursion to traverse the tree to
the parent node.
Display the new tree with the new node added. This should be done
using a
recursive method in your project ( method printTree() ).
Display the menu.
If the parent node has no children, use recursion to add the new
node as the first child
(left child) of the parent node.
Display the new tree with the new node added.
Display the menu.
o If the user wants to know the size of the tree (i.e., the number of nodes in the whole tree or a
sub-tree), request for the name or ID of the node that is the
root of the desired tree (or 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).
Recursively count the number of nodes in the tree (or sub-tree)
and display this with
an appropriate message.
Display the tree (or subtree) whose size was just
displayed.
Display the menu.
o If the user wants to find a node, request for the name or ID of
the node to search for in the
tree.
Search for the node in the tree using recursive calls. If the
node is found, display an
appropriate message; otherwise, indicate that the node does not
exist in the tree.
Display the menu.
o If the user wants to exit, terminate the program.
A sample output (to be followed exactly) is included below.
Example Output
A B C
B D E
D H I
H
I
E J K
J
K
C F G
F L
L
G
There are 12 nodes in this tree.
Please select one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->.3
Please enter an integer
k
Please enter an integer
6
Invalid input!
Please select 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->
M
Please input the parent node of M->
A
Parent already has 2 children.
Please select 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->
M
Please input the parent node of M->
F
Node successfully added!
A B C
B D E
D H I
H
I
E J K
J
K
C F G
F L M
L
M
G
Please select one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->2
Please input the root node->
A
There are 13 nodes in that tree
A B C
B D E
D H I
H
I
E J K
J
K
C F G
F L M
L
M
G
Please select one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->2
Please input the root node->
C
There are 5 nodes in that tree
C F G
F L M
L
M
G
Please select one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->3
Please input the node you want to find->
G
Node G found!
G
Please select one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->3
Please input the node you want to find->
U
Node U does not exist.
Please select one of the following options:
1. Add Node
2. Tree Size
3. Find Node
0. Exit
->0
Design Requirements
The main class (with the main method) in your application should be
named BinaryTreeDemo. It is provided in
its entirety below and should not be changed.
public class BinaryTreeDemo {
public static void main(String[] args) {
Controller controller = new Controller();
controller.manipulateTree();
}
}
You should have another class – Controller – whose object is used
to call methods to create the initial tree and
then manipulate it depending on the user’s selections from the
menu. The constructor for the Controller class
does the following:
instantiates an object of the TreeDataStructure class (this
object is the root of the tree), and
calls one of the Controller class methods: “createTree()” (given
below).
public void createTree() {
root.addChild("B", "A");
root.addChild("C", "A");
root.addChild("D", "B");
root.addChild("E", "B");
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");
root.printTree();
System.out.println("There are " + root.size() + " nodes in this
tree.\n");
}
The Controller class should also have a method (manipulateTree())
which contains the code to loop as long as
the user wants to, and perform the tasks desired by the user. The
Controller class should also have a method to
display the menu to the user.
The TreeDataStructure class should implement the ITreeDataStructure
interface (which is provided below).
Your TreeDataStructure class should have two constructors: one
which accepts only one String parameter (used
to create the root node which has no parent) and a second one which
accepts 2 parameters: a String (the ID of
the new node being created, and an ITreeDataStructure object (the
parent of this new node). The methods
addChild(), find(), printTree(), and size() in the
TreeDataStructure class must be implemented using recursion.
public interface ITreeDataStructure {
/**
* This method checks to see if a node with the parentID exists in
the tree.
* If not, it returns false after printing an appropriate message.
If the
* node exists, the method checks to see if it already has two
children. If
* it does the method returns false. Otherwise, it either adds the
new node
* as the parent node’s left child (if the parent has no children)
or as the
* right child (if the parent already has one child). A recursive
approach is
* used.
*
* @param ID The ID of the new node to be added to the tree.
* @param parentID The ID of the parent of the new node.
* @return true if new node was added successfully, false
otherwise.
*/
public boolean addChild(String ID, String parentID);
/**
* This method looks within the tree to find if “value” (the ID of
the node
* to be found) is contained in that subtree. As the search
progresses down
* the tree different nodes will be calling find().
*
* @param value a string (ID of a node) to be found in the
tree
* @return the node, if found.
*/
public ITreeDataStructure find(String value);
/**
* Returns the parent of this node.
*
* @return the parent of this node.
*/
public ITreeDataStructure getParent();
/**
* Returns the size of the tree.
*
* @return the size of the tree (or subtree) starting from this
node
* all the way down to the leaf nodes.
*/
public int size();
/**
* Returns a string with the IDs of this node and its immediate
children (if
* any) all on one line.
*
* @return a string with the IDs of this node and its immediate
children (if
* any).
*/
public String toString();
/**
* Returns the ID of this node.
*
* @return the String ID of this node.
*/
public String getId();
/**
* Uses recursion (and the toString() method) to print (on a line)
each
* node's ID and those of any children it has.
*/
public void printTree();
}
In: Computer Science
The impact of cyberbullying against teenagers and ways to prevent it
In: Computer Science
1. Theory.
The most obvious way to represent a sequence of objects is simply to list them, one after the other, like this.
|
a |
a |
b |
b |
b |
c |
a |
a |
d |
d |
d |
d |
Note that the same objects often appear many times in a row. This is called a run of those objects. In the example sequence, there is a run of 2 a’s, a run of 3 b’s, a run of 1 c, a run of 2 a’s, and a run of 4 d’s. You can represent a sequence with runs by listing its objects, along with the number of times each object appears. For example, you can represent the sequence shown above like this.
|
a |
b |
c |
a |
d |
|
2 |
3 |
1 |
2 |
4 |
Representing a sequence in this way is called run-length
encoding. If a sequence has long runs, or many runs, then
run-length encoding will represent it more efficiently than simply
listing its objects. However, if a sequence has short runs, or few
runs, then run-length encoding may represent it less
efficiently, because extra space is needed to store the lengths of
the runs.
Since a stack is just a simple kind
of sequence, you can use run-length encoding to implement it. In
this assignment, you will write a Java class called RunnyStack that
implements a stack which uses run-length encoding. Here are some
examples of how it works. Suppose you push an object a on
an empty RunnyStack. Then the stack will look like this, with a run
of 1 a.
|
a 1 |
Now suppose you push b. The stack now looks like this, with a run of 1 b, and a run of 1 a.
|
b 1 |
|
a 1 |
If you push another b on the RunnyStack, then the length of the run on top of the stack is incremented, so the stack looks like this.
|
b 2 |
|
a 1 |
If you push yet another b, then the length of the run at the top of the stack would increase to 3. However, suppose that you pop the RunnyStack instead. Then the length of the run at the top is decremented, so that the stack looks like this.
|
b 1 |
|
a 1 |
If you pop the RunnyStack one more time, then the length of the run on top of the stack is decremented to 0. However, a run of 0 objects is like no run at all, so it vanishes, and the stack looks as it did after the first push.
|
a 1 |
Stacks with run-length encoding are used internally by some compilers and interpreters, because they often push the same objects over and over again.
2. Implementation.
You must write a class called RunnyStack that represents a stack. Your class must implement run-length encoding, as described previously. It must also hold objects of type Base, so it will look like this.
class RunnyStack<Base>
{
⋮
}
Your class must define at least the following methods, as described below. To simplify grading, your methods must have the same names as the ones shown here.
public RunnyStack()
Constructor. Make a new, empty instance of RunnyStack.
public int depth()
Return the depth of the stack: the sum of the lengths of all the runs it holds. This is not necessarily the same as the number of runs it holds, which is returned by the method runs.
public boolean isEmpty()
Test if the stack is empty.
public Base peek()
If the stack is empty, then throw an IllegalStateException. Otherwise, return the Base at the top of the stack.
public void pop()
If the stack is empty, then throw an IllegalStateException. Otherwise, decrement the length of the run on top of the stack. If this leaves a run of zero Base’s on top of the stack, then remove that run.
public void push(Base base)
If the stack is empty, then add a new run of one Base at the top of the stack. If the stack is not empty, then test if base is equal to the object in the run at the top of the stack. If it is, then increment the length of that run. If it isn’t, then add a new run of one base at the top of the stack. Note that base may be null.
public int runs()
Return the number of runs in the stack. This is not necessarily the same as its depth, which is returned by the method depth.
Here are some hints, requirements, and warnings. First, all
these methods must work using O(1) operations, so they are
not allowed to use loops or recursions. You will receive no
points for this assignment if you use loops or recursions in any
way!
Second, your RunnyStack class must
have a private nested class called Run. You must use instances of
Run to implement your stack. Each instance of Run represents a run
of Base’s. You will receive no points for this assignment if
you use arrays in any way! The class Run must have three
private slots that have the following names and types. The slot
base points to the Base that appears in the run. The slot length is
an int that is the length of the run. The slot next points to the
instance of Run that is immediately below this one on the stack, or
to null. It must also have a private constructor that initializes
these slots.
Third, your push method must test
non-null Base’s for equality using their equals methods. It must
use the Java ‘==’ operator only for testing null Base’s. It is
helpful to define an extra private method called isEqual that takes
two Base’s as arguments, and tests if they are equal. If either
Base is null, then isEqual uses ‘==’. If neither Base is null, then
isEqual uses equals.
Fourth, RunnyStack’s methods are not
allowed to print things. If you were writing RunnyStack in the Real
World, then it might be part of some larger program. You don’t know
if that larger program should print things.
In: Computer Science