Question

Q 4(b) 6 Marks] Write the code for a single Java class of your own choice that demonstrates the use of the following features

Q 4(b) 6 Marks] Write the code for a single Java class of your own choice that demonstrates the use of the following features in the Java language: Method overloadingg The use of super and this Identify clearly where in your code each of these points is being demonstrated Q 4(c) [6 Marks] Write an abstract Student class in Java which has a name, id_number, year and programme_code as member variables. Add a constructor to your class and a display() method, which prints out all information relating to a Student. Q 4(d) [10 Marks] Write a new class UndergradStudent, which inherits from the Student class of Q4(c) and has the following features An UndergradStudent has an array of Module objects Each Module has a module code (e.g. EE219), a module name and a constructor and display() method. Show the code for this Module class A constructor which takes an array of Module references as a parameter and also passes initialisation values to its parent's constructor. Override the display() method so that it also lists module information. In the above, avoid any unnecessarily duplication of code, for example, make use of the super reference, where applicable.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

4. b)

i) Method Overloading Code

// Java program to demonstrate working of method
// overloading in Java.

public class Sum {

   // Overloaded sum(). This sum takes two int parameters
   public int sum(int x, int y)
   {
       return (x + y);
   }

   // Overloaded sum(). This sum takes three int parameters
   public int sum(int x, int y, int z)
   {
       return (x + y + z);
   }

   // Overloaded sum(). This sum takes two double parameters
   public double sum(double x, double y)
   {
       return (x + y);
   }

   // Driver code
   public static void main(String args[])
   {
       Sum s = new Sum();
       System.out.println(s.sum(10, 20));
       System.out.println(s.sum(10, 20, 30));
       System.out.println(s.sum(10.5, 20.5));
   }
}

ii) The use of this

// Program to illustrate this keyword
// is used to refer current class
class RR {
   // instance variable
   int a = 10;

   // static variable
   static int b = 20;

   void GFG()
   {
       // referring current class(i.e, class RR)
       // instance variable(i.e, a)
       this.a = 100;

       System.out.println(a);

       // referring current class(i.e, class RR)
       // static variable(i.e, b)
       this.b = 600;

       System.out.println(b);
   }

   public static void main(String[] args)
   {
       // Uncomment this and see here you get
       // Compile Time Error since cannot use
       // 'this' in static context.
       // this.a = 700;
       new RR().GFG();
   }
}

The use of super

// Program to illustrate super keyword
// refers super-class instance

class Parent {
   // instance variable
   int a = 10;

   // static variable
   static int b = 20;
}

class Base extends Parent {
   void rr()
   {
       // referring parent class(i.e, class Parent)
       // instance variable(i.e, a)
       System.out.println(super.a);

       // referring parent class(i.e, class Parent)
       // static variable(i.e, b)
       System.out.println(super.b);
   }

   public static void main(String[] args)
   {
       // Uncomment this and see here you get
       // Compile Time Error since cannot use 'super'
       // in static context.
       // super.a = 700;
       new Base().rr();
   }
}

Add a comment
Know the answer?
Add Answer to:
Q 4(b) 6 Marks] Write the code for a single Java class of your own choice that demonstrates the use of the following features in the Java language: Method overloadingg The use of super and this Ident...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • Please write in Java Language Write an abstract class Shape with an attribute for the name...

    Please write in Java Language Write an abstract class Shape with an attribute for the name of the shape (you'll use this later to identify a particular shape). Include a constructor to set the name of the shape and an abstract calculateArea method that has no parameters and returns the area of the shape. Optionally, include a constructor that returns the name of the shape and its area (by calling the calculateArea method). Write a Rectangle class that inherits from...

  • write in java code. oop. i dont have its written code. i have its output in...

    write in java code. oop. i dont have its written code. i have its output in the thrid picture and how the inheritance works with the second picture. just need it in full written code and uni class will be. jus need you to write the whole code for me as its an extra question which i need for my preparation of my exam. add as you but it must be correct and similar to the question Inheritance Do the...

  • Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides...

    Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides the following. • Two attributes: lastName and first Name of type String. • Appropriate initialization, accessors/mutator methods. 2. (6 marks) Write a Student class that is a subclass of Person class (in the previous question) which provides the following. • Two attributes: A unique student ID of type String and GPA of type float; the student ID is initialized when a Student object gets...

  • Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a...

    Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a float Speed and string Name, an abstract method Drive, and include a constructor. Create a Tesla class - Tesla inherits from Vehicle. - Tesla has an int Capacity. - Include a constructor with the variables, and use super to set the parent class members. - Override Drive to increase the speed by 1 each time. - Add a toString to print the name, speed,...

  • Question 3 (CLOS: 3.5) Marks: 4+4 = 8 (a) Write a code in java to make...

    Question 3 (CLOS: 3.5) Marks: 4+4 = 8 (a) Write a code in java to make a class called Animal. The class has data attributes called name. Make one constructor which initializes value of name attribute (b) Make a class called Cat and make it children of Animal class. Declare a variable of the class called age. Make a constructor of this class which initializes both age and name of the class. Make a main method in the class and...

  • IN JAVA Write a class Store which includes the attributes: store name, city. Write another class...

    IN JAVA Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...

  • Write a complete Java program to do the following Your code should have: A super class...

    Write a complete Java program to do the following Your code should have: A super class named Home Properties address SF (this is the number of Square feet) value (this is the dollar value of the home) PPSF (price per square foot) you will calculate this in a method Methods Set and get methods for all properties CalcPPSF - (PPSF is the value/SF) A sub class named Condo, which has one more HOA property: Property: HOAfee (home owners association fee)...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • For the code below write a public static main() method in class Student that: - creates...

    For the code below write a public static main() method in class Student that: - creates an ArrayList<Student> object called students - adds 4 new Student objects to the students list, with some made up names and dates - sort the students list by name and display the sorted collection to System.out. use function getCompByName() - sort the students list by enrollment date and display the sorted collection to System.out. use function getCompByDate() import java.util.Comparator;    import java.util.Date;    public...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT