Click here to load reader

Asg JAVA 1

  • View
    217

  • Download
    0

Embed Size (px)

Text of Asg JAVA 1

  • 8/4/2019 Asg JAVA 1

    1/13

    TCS2044/SCS2034 JAVA PROGRAMMINGASSIGNMENT 1MAY 2011

    Due date: 26 August 2011

    Weighting: Part of 15% of overall assessment.

    Environment: You are required to do this assignment in Java environment. You may use

    pieces of code from the lectures, books, and elsewhere giving appropriatereferences.

    AssessmentYour assignment will be assessed for the following:Correctness of the programsSample test data/results/output or discussion of resultsSubmissionYou are required to submit documentation in the form of printed copy of your codes and

    sample test data. Your submission should bind together with the assignment cover given atthe end of this assignment question. Use YELLOW colour paper as your Assignment 1cover.

    Warning

    1. You are required to do the assignment on your own.

    2. Plagiarism or late submission will be penalised.

  • 8/4/2019 Asg JAVA 1

    2/13

    Question 1

    Define a class named Circle with the following properties:

    A data field named radius of type double with protected access modifier, and a

    String data field named colour with private access modifier. Both data fieldsspecify the radius and the colour of the Circle object respectively.

    A private static constant named PI with a fixed value of 3.142 that represents

    the value of pi.

    A default Circle constructor.

    A constructor that creates Circle objects with the given radius value as the

    argument.

    A public method to display the Circle objects data fields.

    A public method named changeColour(String) to change the value of the

    Circle objects colour based on the value in the method argument.

    A public method named getSurfaceArea() that calculates and returns thesurface area of the Circle object. Use the constant PI value for the calculation.

    (a) Write another class named TestCircle to test the Circle program by creating twoCircle objects. The first object must be created using the default constructor, while

    the second object must be created with the radius value of 10.

    (b) Change the second Circle objects colour to red through thechangeColour() method.

    (c) Display the properties of both objects and also their surface area through their

    respective methods. (15 marks)

  • 8/4/2019 Asg JAVA 1

    3/13

    public class Circle {

    protected double radius;

    private String colour;

    private static double pi= 3.142;

    Circle (){}

    Circle (double radius, String colour) {}

    Circle (double r){

    this.radius = r;

    }

    public void display (){

    System.out.println ("Radius is " + this.radius);

    System.out.println ("Colour is " + this.colour);

    System.out.println ("PI is " + Circle.pi);

    }

    public void changeColour (String c){

    this.colour = c;

    }

    public double getSurfaceArea (){

    return(Circle.pi*(this.radius*this.radius));

    }

    }

    Sample Output

  • 8/4/2019 Asg JAVA 1

    4/13

    public class TestCircle {

    public static void main (String args []){

    Circle c1 = new Circle ( );Circle c2 = new Circle (10.0);

    c1.display ();

    c2.changeColour ("red");

    System.out.println ("");

    System.out.println ("Properties in Circle 1");

    c1.display();

    System.out.println ("Surface area of Cylinder 1: " + c1.getSurfaceArea());

    System.out.println ("");

    System.out.println ("Properties in Circle 2");

    c2.display ();

    System.out.println ("Surface area of Cylinder 2: " + c2.getSurfaceArea());

    }

    }

    Sample Output

  • 8/4/2019 Asg JAVA 1

    5/13

    Question 2:

    Define a class named Cylinder that inherits the properties of the class Circle in

    Question 1. The Cylinder class also contains:

    A private data field named height of type double with the default value 1.0.

    A default Cylinder constructor.

    AnotherCylinder constructor that initializes the Cylinder objects radius and

    height through the constructor arguments.

    A public method named display() to display the Cylinder objects data.

    A public method named getSurfaceArea() that calculates and returns the

    surface area of the Cylinder object.

    A public method named getVolume() that calculates and returns the Cylinder

    objects volume.

    (a) Write another class named Test where an object Circle and an object Cylinderwill be created based on the input given the user. Use the properJOptionPane

    method to get the input from the user.

    (b) Display the properties of both created objects through the display() methods.

    (c) Calculate and display surface area of the Circle object using thegetSurfaceArea() method.

    (d) Calculate and display the surface area and volume of the Cylinder object using thegetSurfaceArea()and getVolume()methods respectively.

    (15 marks)

  • 8/4/2019 Asg JAVA 1

    6/13

    public class Cylinder extends Circle{

    private double height = 1.0;private static double pi= 3.142;

    Cylinder(){}

    Cylinder (double r,double d){

    super.radius = r;this.height = d;

    }

    public void display (){

    System.out.println ("Radius is " + super.radius);System.out.println ("Height is " + this.height);}

    double getSurfaceAreaCylinder (){return((2*Cylinder.pi*super.radius*this.height)+ 2*Cylinder.pi*super.radius*this.height);}

    double getVolumeCylinder(){return((2*Cylinder.pi*(super.radius*this.height))+ 2*Cylinder.pi*super.radius*this.height);}

    }

    Sample Output

  • 8/4/2019 Asg JAVA 1

    7/13

    import javax.swing.JOptionPane;

    public class Test {

    public static void main (String [] args){double radius, height;

    //get the input from user//

    radius=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Radius : "));height=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Height: "));

    //create objectrequired//Circle c1 = new Circle (radius);Cylinder cy1 = new Cylinder (radius, height);

    //display the surface area for circle objects//System.out.println ("The Circle");c1.display();System.out.println ("Surface area of Circle: " + c1.getSurfaceArea());

    System.out.println ("");

    //display the surface area for cylinder objects//System.out.println ("The Cylinder");cy1.display ();System.out.println ("Surface area of Cylinder: " + cy1.getSurfaceAreaCylinder());

    }}

  • 8/4/2019 Asg JAVA 1

    8/13

    Sample Prompt & Output

  • 8/4/2019 Asg JAVA 1

    9/13

    Question 3:

    Write a Java program named TestArray that will declare an array of FOUR (4)Circle

    objects named circle based on the Circle class definition in Question 1.

    (a) Using for orwhile loop and the suitable method from the JOptionPane class, getthe input from user to create FOUR (4)Circle objects and reference the objects to

    the array.

    (b) Change the colour of the THIRD Circle object in the array using thechangeColour() method. The colour will be obtained from the user through the

    JOptionPane input dialog box.

    (c) Use another loop to display the data of all these FOUR (4) objects in the array usingthe display() method.

    (10 marks)

    import javax.swing.JOptionPane;

    public class TestArray{public static void main(String args[]){Circle[] c1=new Circle[4];

    for(int i=0; i

  • 8/4/2019 Asg JAVA 1

    10/13

    Sample Output

  • 8/4/2019 Asg JAVA 1

    11/13

  • 8/4/2019 Asg JAVA 1

    12/13

    Question 4:

    (a) Name the keyword that set an identifier as a constant.

    PI = 3.142(1 mark)

    (b) What are the default value ofradius and colour of the Circle class?

    Radius is 0.0Colour is null (2 marks)

    (c) Briefly explain the modifierpublic, protected, private and static.

    Public is used to defines a class, methods and data and all programs can accessthem.

    Static can be used to define data and methods and it represents class wideinformation that is shared by all instances of the class.

    Protected are used todefines methods and data and any class in the same packageor any subclasses of that class can access them.

    Private can be used to defines methods and data and can be accessed by thedeclaring class only

    (4 marks)

    (d) Name the data field in class Circle that is not inherited by class Cylinder.

    Provide the reason why the property is not inherited?

    Colour, for colour has been declared as private in which only the class or subclasswith the same modifier can call for it

    (2 marks)

    (e) What is the default value of an array of objects?

    Data Colour for Circle 1, Circle 2, Circle 4 is null(1 mark)

    END OF ASSIGNMENT 1 QUESTION PAPER

  • 8/4/2019 Asg JAVA 1

    13/13

Search related