assume that the variables gpa deanslist and studentname

Question 1

1.Assume that the variables gpa, deansList and studentName, have been initialized. Write a statement that adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5

2.Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002. Given a variable modelYear and a String modelName write a statement that prints the message “RECALL” to standard output if the values of modelYear and modelName match the recall details.

3.[Functions >> functions and if statements] Write the definition of a function powerTo which recieves two parameters, a double and an integer. If the second parameter is positive, the function returns the value of the first parameter raised to the power of the second. Otherwise, the function returns 0.


Question 2

Convert each pair of decimal numbers to BCD, and add as indicated:
(a) 4 + 3 (b) 5 + 2 (c) 6 + 4 (d) 17 + 12
(e) 28 + 23 (f) 65 + 58 (g) 113 + 101 (h) 295 + 157
Question 3

Code In Java Netbeans

A University wishes to keep information on its students. The proposed Student class has the following instance variables: studentNo: String, studentName: String, dateOfBirth: Date, meritPoints: Integer. Merit Points represents the entry qualification achieved by a student, which is a number between 20 and 200. A class variable is also required, called noOfStudents, which will be incremented each time a Student instance is created. Write Java code to perform the following:
a) Show the declaration of the Student class, including any setter and getter methods. Declare two constructors as follows; both constructors should increment the class variable appropriately: a. The first is a default constructor that has no parameters and sets the instance variables to either “not known” for the strings, 20 for the integer and your date of birth for the date (assume there is a Date constructor that accepts dates in a string format). b. The second takes 4 parameters, one for each of the instance variables.


Q) The Student class is extended by two sub classes Undergraduate with an additional attribute testScore, and Postgraduate with an additional attribute GPA. Both the classes implement a calculateMerit() method that returns the overall merit of the student. The undergraduate merit is calculated by adding the test score to the merit points, while for the postgraduate GPA is added to the merit points. The sub classes’ constructor should invoke the super class’s constructor and the sub classes’ toString method should invoke the super class’s toString method.

A) Create a test class that creates an array of Student variables to store references to various Student objects.

B) Use enhanced for loop to display the content of each object.

C) At run time, add 10 to the total merit of the undergraduate student.



Answer to question 1

1.

if gpa > 3.5 :
  deanList = deanList + 1
  print (studentName)

The complete copyable code to show the working:

# Declare and initialize variables
gpa = 4.5                   
deanList = 3              
studentName = “Lilly May”   

# Check if gpa exceeds 3.5
if gpa > 3.5 :
  # Add 1 to deanList
  deanList = deanList + 1
  # Print Student name
  print (studentName)
  
# End of program

Screenshot of the source code:

The cmplete copyable code to show the working:

# Define function powerTo
def powerTo(double,int) :
  if int > 0 :
    return double**int
  else:
    return 0
    
print(powerTo(3.5,7));

# End of program    

Screenshot of the output:


Answer to question 2

Step 1

Converting the numbers to BCD and adding them

Step 2

(a) 4 + 3 = 7

BCD of 4 is 100
BCD of 3 is 011
So, 100 + 011 = 0111

(b) 5 + 2 = 7

BCD of 5 is 0101
BCD of 2 is 10
So, 0101 + 10 = 0111

(c) 6 + 4 = 10

BCD of 6 is 0110
BCD of 4 is 100
So, 0110 + 100 = 01010

(d) 17 + 12 = 29

BCD of 17 is 010001
BCD of 12 is 01100
So, 010001 + 01100 = 011101

(e) 28 + 23 = 51

BCD of 28 is 011100
BCD of 23 is 010111
So, 011100 + 010111 = 0110011

(f) 65 + 58 = 123

BCD of 65 is 01000001
BCD of 58 is 0111010
So, 01000001 + 0111010 = 01111011

(g) 113 + 101 = 214

BCD of 113 is 01110001
BCD of 101 is 01100101
So, 01110001 + 01100101 = 011010110

(h) 295 + 157 = 452

BCD of 295 is 0100100111
BCD of 157 is 010011101
So, 0100100111 + 010011101 = 0111000100


Answer to question 3

Step 1

Code:

import java.util.Date;
class Student
{
    String studentNo, studentName;
    Date dateOfBirth;
    int meritPoints;
    static int noOfStudents = 0;
    
    //default constructor
    public Student()
    {
        studentNo = “not known”;
        studentName = “not known”;
        meritPoints = 20;
        dateOfBirth = new Date();
        noOfStudents++;
    }
    //Parametrised constructor
    public Student(String num, String name, int mp, Date db)
    {
        studentNo = num;
        studentName = name;
        meritPoints = mp;
        dateOfBirth = db;
        noOfStudents++;
    }
    
    //Getter methods
    public String getNumber()
    {
        return studentNo;
    }
    public String getName()
    {
        return studentName;
    }
    public int getMP()
    {
        return meritPoints;
    }
    public Date getDOB()
    {
        return dateOfBirth;
    }
    
    //Setter methods
    public void setNumber(String n)
    {
        studentNo = n;
    }
    public void setName(String n)
    {
        studentName = n;
    }
    public void setMP(int n)
    {
        meritPoints = n;
    }
    public void setDOB(Date n)
    {
        dateOfBirth = n;
    }
    
    //toString() method
    public String toString()
    {
        String str = “\nStudent Number: “+studentNo+”\nStudent Name: “+studentName+”\nDOB: “+dateOfBirth+”\nMerit Points: “+meritPoints;
        return str;        
    }
}

class Undergraduate extends Student
{
    int testScore;
    
    //Constructor
    Undergraduate(String num, String name, int mp, Date db, int ts)
    {
        super(num,name,mp,db);
        testScore = ts;
    }
    
    //calculateMerit() method that returns the overall merit of the student
    public int calculateMerit()
    {
        int total = testScore + getMP();
        return total;
    }
    
    //toString() method
    public String toString()
    {
        String str = super.toString() + “\nTotal Points: “+calculateMerit();
        return str;        
    }
}

class Postgraduate extends Student
{
    int GPA;
    //Constructor
    Postgraduate(String num, String name, int mp, Date db, int g)
    {
        super(num,name,mp,db);
        GPA = g;
    }
    
    //calculateMerit() method that returns the overall merit of the student. 
    public int calculateMerit()
    {
        int total = GPA + getMP();
        return total;
    }
    
    //toString() method
    public String toString()
    {
        String str = super.toString() + “\nTotal Points: “+calculateMerit();
        return str;        
    }
}

public class Main
{
 public static void main(String[] args) 
 {
  Undergraduate u = new Undergraduate(“123″,”ABC”, 30, new Date(2000, 11, 21),10);
  Postgraduate p = new Postgraduate(“456″,”XYZ”, 35, new Date(1999,12,12),5);
  Student st[] = new Student[2];
  st[0] = u;
  st[1] = p;
  
  
  for(int i = 0;i<2;i++)
  {
      System.out.println(st[i]);
  }
 }
}

Step 2

Output:


Leave a Comment