Question: Using JAVA: CS1400 Fall 2017 Instructions: You may rework the following problems for 1/2 credit on each. You must get the problem completely correct for
Using JAVA:
CS1400 Fall 2017
Instructions: You may rework the following problems for 1/2 credit on
each. You must get the problem completely correct for additional credit.
Extra credit will be given on a per question basis.
Problems that can be worked: (4, 11, 12, 13, 14, 17, 21, 24, 25, 26, 27,
28, 30, 31)
4. Assume you are in the main method in a MouseDriver class.
a) Write a statement that creates a Mouse object to represent your
personal pet mouse, whom you called "Sweetie".
b) Write another statement that uses a getWeight method (defined in the
Mouse class) to retrieve Sweetie's current weight, and then assign
that retrieved value to a previously declared local variable called
weight.
11. Assume a Car class includes an equals method, whose heading looks like
this:
public boolean equals(Car car)
a) Write a Java expression that uses this method to compare two Car
objects, car1 and car2.
b) Assuming the Car class defines just two instance variables, make and
model, write Java code that implements this equals method.
12. Suppose a CarDriver program has already created and fully initialized
an instance called car1 of a class Car, which defines two private instance
variables, make and model. Then suppose CarDriver creates a copy of this
car with these two statements:
Car car2 = new Car();
car1.copyTo(car2);
Write Java code to implement this copyTo method.
13. Assume you have a Car class that declares two private instance
variables, make and model.
Write Java code that implements a two-parameter constructor that
instantiates a Car object and initializes both of its instance variables.
14. Assume a Car class already has a two-parameter constructor that
initializes make and model attributes. Write Java code that implements an
additional constructor that has three parameters - two String parameters
to initialize the same make and model attributes initialized by the two-
parameter constructor, plus another int parameter to initialize a miles
variable. Your code should utilize the two-parameter constructor to
implement those operations which the two-parameter constructor is already
able to perform.
17. Assume that a class called Cat includes a method with this header:
public static int getCatCount()
Write a Java statement that retrieves the current cat count and assigns it
to an already-declared local variable called catCount in the main method
of the CatDriver class.
21. Write a single statement that copies all the elements in
char[] arr1 = {'x','y','z'};
to the last elements of
char[] arr2 = new char[26];
24. Write a Java statement that declares an array of char elements called
vowels and initializes it with
a
,
e
,
i
,
o
, and
u
.
25. Assume code that has imported Scanner and instantiated it for keyboard
input as stdIn. Write a code fragment that does the following:
a) Declares but does not initialize roots to be an array of doubles.
b) Prompts the user to enter the number of elements and then initializes
roots with an array object whose length equals the input.
c) Uses a for loop to fill each array element with the square root of
that element's index value.
27. Suppose you want a Java program to represent this table of int values:
16 -3 11
27 49 -7
a) Write a single Java statement that declares a two-dimensional array
of int's called numberTable and initializes it so that its element
values correspond to the above table.
b) Write a Java code fragment that prints the values in the numberTable
array as they appear above, where each number in a given row is
preceded by a tab. But make your for loop upper limits able to
accommodate an arbitrary number of rows and an arbitrary number of
columns in each row.
28. Assume the existence of a class called Student which defines two
instance variables:
private String name;
private char gender; // 'M' for male or 'F' for female
Assume this class implements these two methods:
public char getGender()
public String toString() // name and gender
Given an array of Student elements called students, write a for-each loop
that searches the array for male students only and prints each male
student's information on a separate line. Allow for the possibility that
some of the elements in the array may be null.
30. Write a Java statement that removes the Student element currently at
index position i in an ArrayList called students.
31. Write the names of three methods implemented by both ArrayList and
LinkedList classes. [Hint: both of these classes implement the List
interface, which specifies 25 distinct methods.]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
