13
TCS2044/SCS2034 JAVA PROGRAMMING ASSIGNMENT 1 MAY 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 appropriate references. Assessment Your assignment will be assessed for the following: Correctness of the programs Sample test data/results/output or discussion of results  Submission You 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 at the end of this assignment question. Use  YELLOW colour paper as your Assignment 1 cover . Warning 1. You a re req uired to do the a ssig nmen t on yo ur own. 2. Plagiarism or late submission will be penalised.

Asg JAVA 1

Embed Size (px)

Citation preview

Page 1: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/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 results SubmissionYou 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.

Page 2: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/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 object’s data fields.

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

Circle object’s 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 two

Circle 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 object’s colour to “red” through the

changeColour() method.

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

respective methods. (15 marks)

Page 3: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/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

 

Page 4: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/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

Page 5: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/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.

• Another Cylinder constructor that initializes the Cylinder object’s radius and

height through the constructor arguments.

• A public method named display() to display the Cylinder object’s 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

object’s volume.

(a) Write another class named Test where an object Circle and an object Cylinder

will be created based on the input given the user. Use the proper JOptionPane

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 the

getSurfaceArea() method.

(d) Calculate and display the surface area and volume of the Cylinder object using the

getSurfaceArea()and getVolume()methods respectively.

(15 marks)

Page 6: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/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

Page 7: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/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());

} }

Page 8: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/asg-java-1 8/13

Sample Prompt & Output

Page 9: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/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 or while loop and the suitable method from the JOptionPane class, get

the 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 the

changeColour() 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<c1.length; i++)c1[i]=new Circle(Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Circle "+

(i+1)+" Radius: ")));

c1[2].changeColour(JOptionPane.showInputDialog(null,"Enter Circle 3 Colour: "));

for(int i=0; i<c1.length; i++)c1[i].display();}}

Page 10: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/asg-java-1 10/13

Sample Output

Page 11: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/asg-java-1 11/13

Page 12: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/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 of radius and colour of the Circle class?

Radius is 0.0Colour is null (2 marks)

(c) Briefly explain the modifier public, 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 to defines 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

Page 13: Asg JAVA 1

8/4/2019 Asg JAVA 1

http://slidepdf.com/reader/full/asg-java-1 13/13