PROGRAMMING FUNDAMENTALS – SAMPLE PRACTICAL TEST 1
ALL ANSWERS ARE TO BE WRITTEN ON THIS PAPER
PART A
Below is some code for the classes Student and Tutorial. You are required to add your own code to these classes and answer the following questions:
Q1. In the class Student, declare the class attributes id, name, numSubjects. Select the most appropriate data types for these attributes.
Q2. Create an alternate constructor for class Student that has 3 input parameters, one for each of the class attributes listed above. Initialise the class attributes to these input parameter values in the constructor of class Student.
Q3. In the class Student, create a procedure called displayName() that will display the name of the Student.
Q4. In the class Student, create a procedure called displayDetails() that will display the id and name of the Student. Both are to be displayed on one line.
Q5. In the class Student, create a function called updateSubjects() that will ask the user of the program to enter the number of subjects the Student is studying (use Scanner). The value must be entered as integer and returned by the function.
WRITE THE CODE FOR CLASS STUDENT ON THE NEXT PAGE
class Student
{
}// End of class Student
Q6. In the main method of class Tutorial, declare and create two Student objects (so two Student programs are loaded and running), call them anything you like. You must use the alternate constructor for Student to do this.
Q7. In the main method of class Tutorial, after creating the two student objects, call the procedure displayName() for each student object.
Q8. Now add extra code in the main method that calls the updateSubjects() function for each student object. The values returned from the function calls must be stored in two local variables. Use these variables to determine which student is studying the most subjects, then call displayDetails() for this student only.
WRITE THE CODE FOR CLASS TUTORIAL ON THE NEXT PAGE
class Tutorial
{
public static void main(String args[])
{
}
}// End of class Tutorial
PART B
Q1. Write down the correct way to declare and create an object using the following Constructor method headers:
Laptop(String model, double version, double cost)
Keyboard(int id, String type)
Account(int number, boolean current, double balance)
Phone(String name, double price, String code)
Car(String registration, double mileage, double cost)
Q2. What is wrong with this code? Correct all errors so the code will compile. More than one solution is possible.
Double amount = “3.75”;
if(amount = 100 || amunt => 50);
{
System.out.println("Amount is: " + amount);
}
else (amount == 3.75)
{
amount = 5.2;
System.out.println("Amount is: " + amount);
}
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。