Programming Questions and Exercises : Class II

Question 1

Consider the following definition of the class MyClass:

public class MyClass
{
   private static int count = 0;
   private int x;

   public MyClass(int i)
   {
      x = i;
   }
   public void incrementCount()
   {
      count++;      
   }

   public void printX()
   {
      System.out.println("Value of x : "  + x);
   }

   public static void printCount()
   {
      System.out.println("Value of count : "  + count);
   }
}

public class MyClassDemo
{
   public static void main(String[] args)
   {
        MyClass myObject1 = new MyClass(5);
        MyClass myObject2 = new MyClass(7);        
   }
}

What is the output of the following Java code? (Assume that following statements are written inside main)

myObject1.printX();
myObject1.incrementCount();
MyClass.incrementCount();
myObject1.printCount();
myObject2.printCount();
myObject2.printX();
myObject1.setX(14);
myObject1.incrementCount();
myObject1.printX();
myObject1.printCount();
myObject2.printCount();

 

Question 2

Write a Java class Clock for dealing with the day time represented by hours, minutes, and seconds. Your class must have the following features:

  • Three instance variables for the hours (range 0 - 23), minutes (range 0 - 59), and seconds (range 0 - 59).
  • Three constructors:
    • default (with no parameters passed; is should initialize the represented time to 12:0:0)
    • a constructor with three parameters: hours, minutes, and seconds.
    • a constructor with one parameter: the value of time in seconds since midnight (it should be converted into the time value in hours, minutes, and seconds)
  • Instance methods:
    • set-method method setClock() with one parameter seconds since midnight (to be converted into the time value in hours, minutes, and seconds as above).
    • get-methods getHours(), getMinutes(), getSeconds() with no parameters that return the corresponding values.
    • set-methods setHours(), setMinutes(), setSeconds() with one parameter each that set up the corresponding instance variables.
    • method tick() with no parameters that increments the time stored in a Clock object by one second.
    • method addClock() accepting an object of type Clock as a parameter. The method should add the time represented by the parameter class to the time represented in the current class.
    • Add an instance method toString() with no parameters to your class. toString() must return a String representation of the Clock object in the form "(hh:mm:ss)", for example "(03:02:34)".
    • Add an instance method tickDown() which decrements the time stored in a Clock object by one second.
    • Add an instance method subtractClock() that takes one Clock parameter and returns the difference between the time represented in the current Clock object and the one represented by the Clock parameter. Difference of time should be returned as an clock object.

Write a separate class ClockDemo with a main() method. The program should:

  • instantiate a Clock object firstClock using one integer seconds since midnight obtained from the keyboard.
  • tick the clock ten times by applying its tick() method and print out the time after each tick.
  • Extend your code by appending to it instructions instantiating a Clock object  secondClock by using three integers (hours, minutes, seconds) read from the keyboard.
  • Then tick the clock ten times, printing the time after each tick.
  • Add the secondClock time in firstClock by calling method addClock.
  • Print both clock objects calling toString method
Create a reference thirdClock that should reference to object of difference of firstClock and secondClock by calling the method subtractClock().

 

Question 3

Write a Java class Complex for dealing with complex number. Your class must have the following features:

  • Instance variables :
    • realPart for the real part of type double
    • imaginaryPart for imaginary part of type double.
  • Constructor:
    • public Complex (): A default constructor, it should initialize the number to 0, 0)
    • public Complex (double realPart, double imaginaryPart): A constructor with parameters, it creates the complex object by setting the two fields to the passed values.
  • Instance methods:
    • public Complex add (Complex otherNumber): This method will find the sum of the current complex number and the passed complex number.  The methods returns a new Complex number which is the sum of the two.
    • public Complex subtract (Complex otherNumber): This method will find the difference of the current complex number and the passed complex number.  The methods returns a new Complex number which is the difference of the two.
    • public Complex multiply (Complex otherNumber): This method will find the product of the current complex number and the passed complex number.  The methods returns a new Complex number which is the product of the two.
    • public Complex divide (Complex otherNumber): This method will find the ... of the current complex number and the passed complex number.  The methods returns a new Complex number which is the ... of the two.
    • public void setRealPart (double realPart): Used to set the real part of this complex number.
    • public void setImaginaryPart (double realPart): Used to set the imaginary part of this complex number.
    • public double getRealPart(): This method returns the real part of the complex number
    • public double getImaginaryPart(): This method returns the imaginary part of the complex number
    • public String toString(): This method allows the complex number to be easily printed out to the screen
Write a separate class ComplexDemo with a main() method and test the Complex class methods.

 

Question 4

Write a Java class Author with following features:

  • Instance variables :
    • firstName for the author’s first name of type String.
    • lastName for the author’s last name of type String.
  • Constructor:
    • public Author (String firstName, String lastName): A constructor with parameters, it creates the Author object by setting the two fields to the passed values.
  • Instance methods:
    • public void setFirstName (String firstName): Used to set the first name of author.
    • public void setLastName (String lastName): Used to set the last name of author.
    • public double getFirstName(): This method returns the first name of the author.
    • public double getLastName(): This method returns the last name of the author.
    • public String toString(): This method printed out author’s name to the screen

Write a Java class Book with following features:

  • Instance variables :
    • title for the title of book of type String.
    • author for the author’s name of type String.
    • price for the book price of type double.
  • Constructor:
    • public Book (String title, Author name, double price): A constructor with parameters, it creates the Author object by setting the the fields to the passed values.
  • Instance methods:
    • public void setTitle(String title): Used to set the title of book.
    • public void setAuthor(String author): Used to set the name of author of book.
    • public void setPrice(double price): Used to set the price of book.
    • public double getTitle(): This method returns the title of book.
    • public double getAuthor(): This method returns the author’s name of book.
    • public String toString(): This method printed out book’s details to the screen

Write a separate class BookDemo with a main() method creates a Book titled “Developing Java Software” with authors Russel Winderand price 79.75. Prints the Book’s string representation to standard output (using System.out.println).