You are on page 1of 6

CONFIDENTIAL

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

MARKING SCHEME
TEST
SEMESTER I
SESSION 2021/2022

COURSE NAME : OBJECT-ORIENTED


PROGRAMMING
COURSE CODE : BIT 20603
PROGRAMME CODE : BIT
EXAMINATION DATE : JANUARY/FEBRUARY 2022
DURATION : 2 HOURS
INSTRUCTION : ANSWER ALL QUESTIONS.

THIS QUESTION PAPER CONSISTS OF FOUR (4) PAGES

CONFIDENTIAL
CONFIDENTIAL BIT20603

Q1. Answer Q1(a)-Q1(h) based on the information given in in Figure Q1.

A Temperature class is developed to hold a temperature in Fahrenheit


unit. The class shall provide methods to get the temperature in
Fahrenheit, Celsius, and Kelvin units.

The class should have the following field:


ftemp – a double that holds a Fahrenheit temperature.

The class should have the following methods:


 A constructor with no-argument- The constructor is used to store
the value of 0.0 in the ftemp field.
 A constructor with argument-The constructor accepts a Fahrenheit
temperature (as a double) and stores it in the ftemp field.
 setFahrenheit- The setFahrenheit method accepts a Fahrenheit
temperature (as a double) and stores it in the ftemp field.
 getFahrenheit-returns the value of the ftemp fled, as a Fahrenheit
temperature (no conversion required).
 getCelsius-returns the value of the ftemp field converted to
Celsius.
 getKelvin-returns the value of the ftemp field converted to
Kelvin.

Use the following formula to convert the Fahrenheit temperature to


Celsius:
Celsius = (5/9) X (Fahrenheit – 32)
Use the following formula to convert the Fahrenheit temperature to
Kelvin:
Kelvin = ((5/9) X (Fahrenheit – 32)) + 273

A separate program, namely TestTemperature is written to create two


instances of the class Temperature. The TestTemperature shall
demonstrate the use of the two different constructors in class
Temperature. For one of the instances, the program should pass to the
constructor with the value entered by a user.

For both instances, the program should then call the methods to display
the temperature in Fahrenheit, Celsius, and Kelvin.

Figure Q1

2
CONFIDENTIAL BIT20603

(a) Declare the data field for the class with appropriate access specifier.
(C4, 1 mark)

(b) Write a constructor with no argument for the class.


(C4, 2 marks)

(c) Write a constructor with argument for the class.


(C4, 2 marks)

(d) Write the setFahrenheit method.


(C4, 3 marks)

(e) Write the getFahrenheit method.


(C4, 3 marks)

(f) Write the getCelsius method.


(C4, 3 marks)

(g) Write the getKelvin method.


(C4, 3 marks)

(h) Write a complete program for TestTemperature.


(C4, 13 marks)

3
CONFIDENTIAL BIT20603

Q2. Answer Q2(a)-Q2(d) based on the information given in Figure Q1 and Figure Q2.

A separate program, namely TestArrayTemperature is written to create


an array that will keep daily temperature in Fahrenheit unit for a
week. In TestArrayTemperature, an array called tempArray, is created
to compose the objects of class Temperature specified in Figure Q1.
The program shall ask user to enter temperature values for the week.
The input values shall be passed to the constructor to initialize
the data field for each object. The program shall determine and
display the maximum temperature and average temperature for the week.

Figure Q2

(a) Declare and create tempArray.


(C4, 2 marks)

(b) Write a program fragment that will ask user to enter temperature values. The
temperature values shall be used to initialize the data field for each object
referenced in tempArray (Use constructor).
(C4, 8 marks)

(c) Write a program fragment that will determine and display the maximum
temperature of the week.
(C4, 10 marks)

(d) Write a program fragment that will determine and display the average
temperature of the week.
(C4, 10 marks)

- END OF QUESTIONS –

4
CONFIDENTIAL BIT20603

MARKING SCHEME

No. Answer

1(a) private double ftemp; //(a) 1 mark

public Temperature() //(b) 1 mark - header


1(b) { ftemp = 0.0; } // 1 mark - body

public Temperature(double temperature) //(c) 1 mark – header


1(c) { ftemp = temperature; } // 1 mark - body

public void setTemperature(double temperature) //(d) 1 mark - header


1(d) { ftemp = temperature; } //2 marks - body

public double getFahrenheit() //(e) 1 mark - header


1(e) { return ftemp; } //2 marks - body

public double getCelsius() //(f) 1 mark - header


1(f) { return 5/9*(ftemp-32); }// 2 marks - body

public double getKelvin() //(g) 1 mark


1(g) { return (5/9*(ftemp-32))+273; } // 2 marks

import java.util.Scanner; //0.5 marks


1(h) public class TestTemperature {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //0.5 marks
Temperature tempRef1 = new Temperature(); //2 marks

System.out.println("\n\nFirst temperature info:");


System.out.println("Fahrenheit :"+tempRef1.getFahrenheit()); //1 mark
System.out.println("Celsius :"+tempRef1.getCelsius()); //1 mark
System.out.print("Kelvin :"+tempRef1.getKelvin()); //1 mark

System.out.print("\nEnter temperature value: ");


double temp = input.nextDouble(); //2 marks
Temperature tempRef2 = new Temperature(temp);//2 marks

System.out.println("\n\nSecond temperature info:");


System.out.println("Fahrenheit :"+tempRef2.getFahrenheit()); //1 mark
System.out.println("Celsius :"+tempRef2.getCelsius()); //1 mark
System.out.print("Kelvin :"+tempRef2.getKelvin()); //1 mark
}
}

2(a) Temperature[] arrayTemp = new Temperature[SIZE]; //(a) 2 marks

double inputTemp; //(b) 1 mark


2(b) for(int index=0; index<arrayTemp.length; index++) //3 marks
{
System.out.print("\nEnter temperature: ");
inputTemp = input.nextDouble(); //2 marks
arrayTemp[index] = new Temperature(inputTemp); } //2 marks

5
CONFIDENTIAL BIT20603

double maxTemp = arrayTemp[0].getFahrenheit(); //(c) 2 marks


2(c) for(int index=1; index<arrayTemp.length; index++) //3 marks
{
if (arrayTemp[index].getFahrenheit()>maxTemp) //2 marks
maxTemp = arrayTemp[index].getFahrenheit(); //2 marks
}

System.out.println("Maximum temperature (Fahrenheit): "+maxTemp); //1 mark

double total=0.0; //(d)2 marks


2(d) for(int index=0; index<arrayTemp.length; index++) // 3 marks
total += arrayTemp[index].getFahrenheit(); // 2 marks

double average = total/arrayTemp.length; //2 marks


System.out.println("Average temperature (Fahrenheit): "+average); //1 mark

You might also like