B) PROBLEMS ( 30 points) Clearly write your answers in the provided space. Clear
ID: 3839187 • Letter: B
Question
B) PROBLEMS ( 30 points) Clearly write your answers in the provided space.
Clearly write the output of the following statement. (2pts)
Int n = 4;
Int sum = 0;
while (n > 0) {
n = n – 1;
sum = sum + n;
}
System.out.println(“sum = “ + sum);
2)Clearly write the output of the following statement (2 points)
Int sum = 0;
for (int n=1; n < 5; n=n+2) {
sum = sum + n;
}
System.out.println(“sum = “ + sum);
Assume a public method generateOutput is an abstract method in class Product, and this method returns a String value and has a parameter in the type of int.
3.1) Declare this abstract method in the following. Don’t need to write code to define the whole Product class. (2Points)
3.2) Does this Product class have to be defined as abstract? (YES/NO)
yes
3.3) Can you create a child class of this product class by extending it? (YES/NO)
yes
3.4) Can you instantiate an object from this Product class? (YES/NO)
no
3.5) Does every method in this Product class have to be defined as abstract? (YES/NO)
no
Given the following exception hierarchy and code example 1. (4PTS)
Java.lang.Exception
Java.io.IOException
java.io.FileNotFoundException
try
{
// code that throws FileNotFounException or IOException
Statements 1 go here
}
catch (FileNotFoundException e)
{
// code that handles FileNoteFoundException
Statements 2 go here
}
finally
{
Statements 3 go here
}
Statements 4 go here
4.1) If FileNotFoundException is thrown in the try block, will statements 2 in the catch block be executed? (YES/NO)
yes
4.2) If FileNotFoundException is thrown in the try block, will statements 3 in the finally block be executed? (YES/NO)
yes
4.3) If IOException is thrown in the try block, will statements 3 in the finally block be executed? (YES/NO)
yes
4.4) If IOException is thrown in the try block, will statements 4 be executed? (YES/NO_
no
In the following program Test
Public class Test {
Public static void main (String args[])
{
Int n[] = {-1, 2} ;
Int sum = n[0] + n[1];
changeValue(n, sum);
System.out.println(“n[0] = “ + n[0]);
System.out.println(“n[1] = “ + n[1]);
System.out.println(“sum = “ + sum);
} //end of main
Public static void changeValue (int k[], int sum)
{
for (int i = 0; i < k.length; i++)
{
k[i] = k[i] + 1;
sum = sum + k[i];
}
}
} // end of Test
5.1) Whether the method changeValue must be defined as static, and why? (1 Point)
5.2) If the program executed, what is the output of the program? (3 point)
Complete the following Car class according to requirements.
Public class Car {
int year;
String model;
double speed;
//define a constructor which accepts a car’s year and model as arguments and use them to initialize the values for the year and maker attributes. (2 points)
//define a public method, getSpeed, that returns a double value for the instance variable, speed
// and this method does not have an argument. (1 point)
//define public method, setSpeed; that has double type argument (1 point)
//define a public method, goBreak, which does not have any argument and not return anything.
// When this method is called, if the instance variable, speed, is greater than 10, it reduces the speed
// by 10; otherwise, it sets the speed to zero. (2 Pts)
} // end of Car
7)Given the following class diagram, write statements to answer the following questions. (6 points)
7.1) create a MotorVehicle object named mv
MotorVehicle mv = new MotorVehicle();
7.2) for this mv object, assign 4 as number of wheels
7.3) what object oriented concept is demonstrated in the solution for the above question?
7.4) toString() is defined as regular method in both Vehicle and MotorVehicle, what is this phenomenon (of defining the method in both child and parent classes) called?
7.5)
Vehicle v = new MotorVehicle();
System.out.println(v.toString());
In the above statement, toString() defined in which class is called?
7.6) what object-oriented concept the following statement illustrates?
Vehicle v = new MotorVehicle();
Explanation / Answer
The Answers are written for each question after the heading Answers as follows :-
-----------------------------------------------------------------------------------------------------------------------
1) Clearly write the output of the following statement. (2pts)
Int n = 4;
Int sum = 0;
while (n > 0) {
n = n – 1;
sum = sum + n;
}
System.out.println(“sum = “ + sum);
Answer:-
-------------
sum = 6
2)Clearly write the output of the following statement (2 points)
Int sum = 0;
for (int n=1; n < 5; n=n+2) {
sum = sum + n;
}
System.out.println(“sum = “ + sum);
Answer:-
-------------
sum = 4
3)Assume a public method generateOutput is an abstract method in class Product, and this method returns a String value and has a parameter in the type of int.
3.1) Declare this abstract method in the following. Don’t need to write code to define the whole Product class. (2Points)
Answer:-
-----------
class product
{
public abstract String generateOutput(int i)
{
}
}
3.2) Does this Product class have to be defined as abstract? (YES/NO)
yes
3.3) Can you create a child class of this product class by extending it? (YES/NO)
yes
3.4) Can you instantiate an object from this Product class? (YES/NO)
no
3.5) Does every method in this Product class have to be defined as abstract? (YES/NO)
no
4)Given the following exception hierarchy and code example 1. (4PTS)
Java.lang.Exception
Java.io.IOException
java.io.FileNotFoundException
try
{
// code that throws FileNotFounException or IOException
Statements 1 go here
}
catch (FileNotFoundException e)
{
// code that handles FileNoteFoundException
Statements 2 go here
}
finally
{
Statements 3 go here
}
Statements 4 go here
Answer:-
-------------
try
{
// code that throws FileNotFounException or IOException
BufferedReader rd = null;
rd = new BufferedReader(new FileReader(new File(filename)));
}
catch (FileNotFoundException e)
{
// code that handles FileNoteFoundException
System.out.println("The given file is not found");
}
finally
{
rd.close();
}
System.out.println("Hello world");
4.1) If FileNotFoundException is thrown in the try block, will statements 2 in the catch block be executed? (YES/NO)
yes
4.2) If FileNotFoundException is thrown in the try block, will statements 3 in the finally block be executed? (YES/NO)
yes
4.3) If IOException is thrown in the try block, will statements 3 in the finally block be executed? (YES/NO)
yes
4.4) If IOException is thrown in the try block, will statements 4 be executed? (YES/NO)
no
5) In the following program Test
Public class Test {
Public static void main (String args[])
{
Int n[] = {-1, 2} ;
Int sum = n[0] + n[1];
changeValue(n, sum);
System.out.println(“n[0] = “ + n[0]);
System.out.println(“n[1] = “ + n[1]);
System.out.println(“sum = “ + sum);
} //end of main
Public static void changeValue (int k[], int sum)
{
for (int i = 0; i < k.length; i++)
{
k[i] = k[i] + 1;
sum = sum + k[i];
}
}
} // end of Test
5.1) Whether the method changeValue must be defined as static, and why? (1 Point)
Answer:-
Yes, The method changeValue must be defined as static. Because static methods can be used with out creating any object of that class and in the above code we did not create any object in the main method to call the changeValue method.
5.2) If the program executed, what is the output of the program? (3 point)
Answer:-
Output of the program is :-
n[0] = 0
n[1] = 3
sum = 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.