Download Lab9 Solution

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ICS102: Computer Programming
Department of Computer Science & Software Engineering,
University of Hail, Kingdom of Saudi Arabia
Lab 9
Prepared By:Noor ALzaareer
Lab9 objectives
The aims of this lab are to:
- Learn to define class.
- Learn to define class members (instance variable ).
- Learn to create object from class, and refer to a particular instance variable,
Preparatory work
a) In your Z:\ICS102\Labs folder, create a folder called Lab9.
b) In the folder Z:\ICS102\Labs\Lab9 created in the previous step, create the folders
Exercise1, Exercise2 .
c) Open JCreator Software.
Exercise 1
1. Create an empty project named Exercise1 and
Z:\ICS102\Labs\.Lab9\Exercise1.
2. Add to the project a new Java file named Ex1.
3. Complete the following code according to the comment
4. look to the output before you write the code.
// Define class Rectangle
class Rectangle{
save
it
in
//Declare variable length with appropriate data type
double length;
//Declare variable width with appropriate data type
double width;
//Declare variable area with appropriate data type .
double area;
}
public class Ex1{
public static void main(String args[]){
// create object r from Rectangle class.
Rectangle r=new Rectangle();
//use object r to refer to the variable length and assign it to the value 12.5
r.length =12.5;
//use object r to refer to the variable width and assign it to the value 14.0
r.width=14;
// use object r to refer the variable area and assign it to the value ( length*width)
r.area= r.length* r.width;
// print the value of length and width and area for this created object based on the output.
System.out.println("Length of rectangle is " + r.length);
System.out.println("Width of rectangle is " + r.width);
System.out.println("The area is " + r.area);
}}
the
folder
The Output:
Length of rectangle is 12.5
Width of rectangle is 14.0
The area is 175.0
Exercise 2
1. Create an empty project named Exercise2 and save it in the folder
Z:\ICS102\Labs\.Lab9\Exercise2.
2. Add to the project a new Java file named Ex2.
3. Rewrite the above program to allow user to input the value for length and width using
scanner class, by update the code in the main method.
4. Change the name of class to Rectangle2.
5. Look to the Sample Output below.
import java.util.Scanner;
class Rectangle{
double length;
double width;
double area;
}
public class Ex1{
public static void main(String args[]){
Rectangle r=new Rectangle();
Scanner k= new Scanner(System.in);
System.out.println("Enter the Length of Rectangle");
r.length =k.nextDouble();
System.out.println("Enter the width of Rectangle");
r.width=k.nextDouble();
r.area= r.length* r.width;
System.out.println("The length of rectangle is " + r.length);
System.out.println("The width of rectangle is " + r.width);
System.out.println("The area of rectangle is " + r.area);
}}
Sample Output:
Enter the Length of Rectangle
15
Enter the width of Rectangle
10
the width of rectangle is 10.0
the length of rectangle is 15.0
the area of rectangle is150.0
Related documents