Write a class called Wrench with two private variables: an integer

advertisement
1. (15 points) Write a class called Wrench with two private variables: an
integer representing the length of the handle in centimeters, and a double
representing the size of the nut that it can turn.
a. The class should contain a constructor method that takes an integer and a
double as parameters and uses these parameters to initialize the class
variables.
b. Write a method ConvertLength that takes the value of Wrench length
(integer) and returns a value in inches (float) (1inch = 2.54 centimeters).
c. Create an object of the class Wrench in main and call the above method.
public class Wrench
{
// Declare your private variables here.
// Write a constructor that takes two parameters and initializes
// the class variables to the values in the parameters.
// ConvertLength method here
// Write your main method here
public static void main(String args[])
{
// Create an instance of class Wrench
// Call the ConvertLength method
}
1
2. (20 points ) Write a class “student “as described below:
a) The class contains two variables – student id which is an integer and grade
which is a float variable.
b) Write mutator methods that set the value of student_id and grade
c) Write a main method and create an instance of the object student. Read a
student_id as input from the user using EasyIn and set the student_id to that
value.
e) Generate a random float value between 20 and 21 and set the grade to that
value.
f) Print the student id and grade in main
public class student
{
// Declare your variables here.
// Set Method for student_id
// Set Method for grade
// Write your main method here
public static void main(String args[])
{
// Create an instance of student class
// Read a student_id value from user
// Call set method for student_id
// Generate random float value between 20 and 21
2
// Call set method for grade
}
}
3. (10 points ) a. Create a method adder , that reads three double values as
parameters and returns the average of the three numbers as an integer
public int adder ( double d1, double d2, double d3)
{
}
b. What is a narrowing conversion ? What is the kind of conversion that you had
to perform in part (a) ?
4.(20 points ) Here is a class called LightBulb with one variable: an integer
representing how many Watts the light bulb consumes (its wattage).
a) Write a set method SetWattage for the wattage variable
b) Write a get method GetWattage for the wattage variable
c) Write a driver class with a main method that creates an instance of the
LightBulb class
d) From the main method in the driver class, set the wattage of the bulb to 100
e) Use the GetWattage function and print the wattage value
public class LightBulb
{
int wattage;
3
// SetWattage method
//GetWattage method
}
public class driver
{
public void static main(String args[])
{
// Create an instance of class LightBulb
// SetWattage to 100
// Use GetWattage and print the value
}
5. (15 points) Consider the following method.
void method1 ( )
{
int a, b ,
a = 5;
b = 10;
System.out.println("\"Hello World\"");
return a;
}
a) What is the result of a/b?
4
b) What is the result of the print statement ?
c) Identify the error in the method . (Hint : Return type )
d) Can the variables a and b be accessed from another method ? Why/Why not ?
6. (20 points )
(a) (3 points ) Identify the compilation error if any in this program
public class class1
{
public static void main (string args[])
{
final int NUMBER = 10;
NUMBER = 20;
}
}
(b) (3 points) In the following declaration , what is stored in teststr ? The
memory map is shown too.
String teststr = new string(“Hello”);
H
1050
e
1051
l
l
o
(c) (5 points ) Given the declaration of name below, what value is returned
by each method call?
String name = “anne boleyn”;
5
name.indexOf(‘b’);
name.length( );
(d) ( 5 points) Create an object of the Integer Wrapper class. How is this
different from a primitive datatype ? (Hint : Think of the memory map )
(e) (4 points)
6
Download