Posts

Lab Exercise Inheritance

  class Video { { private String title;  private int lenghth; private final boolean avail; public Video ( String ttl, int lentgh ) {     title = ttl; lentgh = 90; avail = true; } public void show(String length) {     System.out.println(title + ", "+ length + " min. available :" + avail );          }      } public class VideoStore {          public static void main ( String args [])     {         Video item1 = new Vieo ("Microcosmos", 90 );         Movie item2 = new Movie ("Jaws", 120, "Spielberg", "PG" );         Movie item3 = new Movie ("Ironman", 120, "Avengers", "PG" );                           item1.show();         item2.show();         item3.show();     }         } public class Movie extends Video  {         private String   director;         private String    rating;                  public Movide( String ttl, int lngth, String dir, String rtg )         {             super( ttl, lngth);    

Exercise User Defined Class 3

Image
TASK 1 a)              Write a complete Student class. public class Student {          private String matricNo;          private double test1;          private double test2;          private double averageMark;                    public Student(String matric, double ts1, double ts2 ){                     this.matricNo = matric;                     this.test1 = test1;                     this.test2 = test2;              }           public String getStudentInfo() {                     return matricNo+"\t\t"+averageMark;           }          public void calculateAverage()           {                     averageMark = (test1 + test2)/2;           }   } b)              Next, write TestStudent class that invokes the methods of the Student class   and applies the concept of an array of objects. public class TestStudent {     public static void main (String [] args) {           Scanner input = new Scanner(System.in);         String matri

Exercise User Defined Class 2

Image
Task 1 Type the above definition of a Triangle class and save in a file. 1.      What is the name to be given to this file? Triangle 2.      Compile this file.  If you get any error, it means that you haven’t typed correctly the given codes. No error detected. 3.      After a successful compilation, now try to run. Explain what happened and why. Cannot run because there is no main method. Task 2 Complete the above Java program and save it in the same folder where the Triangle class file is located.   1.      Compile this file.   If you get any error, it means that you haven’t typed correctly the given codes. import java.util.Scanner; public class TriangleDemo {     public static void main(String[] args)    {       //create a Scanner object       Scanner input = new Scanner(System.in);       // Create a Triangle object.       Triangle triangle = new Triangle();//using java default if takde constructor       // Prompt user to input value for height and base     

Exercise User Defined Class

Image
  1.            What will be the output from the following code?   class QuestionOne {      int count;           public void init(){            count = 1;      }      public void increment() {            count = count + 1;      }      public int getCount() {            return count;      } }       public class Q1Main {         public static void main (String [] arg) {             QuestionOne q1;             q1 = new QuestionOne (1);             q1.increment();             q1.increment();             System.out.println(q1.getCount());         }     } 2.            Read and analyse the following class: class Staff {      private String name, staffID;      private double salary;      private int workingDay;      public void setStaffInfo(String nm, String id, int wDay){            name=nm; stafID=id; workingDay=wDay;      }      pub