PDA

View Full Version : Java questions - I need the answers


m4ssiv3
Feb 10, 2010, 07:22 AM
Declare each of the following:
(a) a field of data type boolean and name isFound;
(b) a local variable of data type String and name carModel;
(c) a field of data type double and name weight;
(d) a field of data type Employee and name secretary.

2. Write a declaration for an array variable temperatures that could be used to refer
to an array of double values.
Give a Java statement to create an array of double elements of size 95 and referenced
by temperatures.
What is the index range for this array?
Write a fragment of code to determine the average temperature in an array, temperatures,
of temperature readings.
3. A class Car has the following fields:
private String registration;
private int doors;
private String colour;
private CarDealer supplier;


(a) Define a class variable, totalCars, whose purpose is to keep track of the total
number of Car objects constructed during execution of a program that uses
the Car class.
(b) Declare a default constructor for this class.
(c) Declare a constructor for this class which has a formal parameter corresponding
to each field.
(d) Declare an accessor method called getColour whose purpose is to return the
value of the colour field of this Car.
(e) Declare a mutator method that sets the registration number of this Car to a
given value.
(f) Declare a method to determine whether this Car has a given colour.

FadedMaster
Feb 10, 2010, 07:30 AM
I would recommend asking your teacher if you need help with homework. ;)

m4ssiv3
Feb 12, 2010, 02:26 PM
I did them, can anyone tell me if I did them correctly. Also I have know idea for (F)

1. (a) public boolean isFound;
(b) String name = carModel;
(c) private double weight;
(d) private Employee secretary;

2. private double [ ] temperatures;

double[ ] temperatures = new double[95];
0-94 (n-1)


double total=0.0, average;
int size = tempArray.length;
for ( int index=0; index < size; index++) {
total += tempArray [ index ];
} // end for
average = total / size;

temperatures [ 0 ] = 0.0;
lastDay = temperatures[ 364 ];


3. A class Car has the following fields:
private String registration;
private int doors;
private String colour;
private CarDealer supplier;

(a) private static int totalCars = 0;


(b) Declare a default constructor for this class.

public Car(String Registration , int doors, String coulour, suppier){

(c) Declare a constructor for this class which has a formal parameter corresponding
to each field.

public Car(String carRegistration , int carDoors, String carCoulour, carSuppier){

registration = carRegistration;
doors = carDoors;
colour = carColour;
supplier = carSupplier;


(d) Declare an accessor method called getColour whose purpose is to return the
value of the colour field of this Car.

public int getColour()
{
return colour;
}

(e) Declare a mutator method that sets the registration number of this Car to a
given value.

public void setRegistration(String newRegistration)
{
registration = newRegistration;
}

(f) Declare a method to determine whether this Car has a given colour.

jmd814
Feb 12, 2010, 11:54 PM
Most of your answers are correct.

However, 3b is wrong - a default constructor has no parameters.

As for 3f. Imagine a method where a color is passed into the method and if the car color the parameter are equal then true is returned, otherwise it returns false.