Assignment 1 Individual Assignment: Mohammad Saad Uddin
Assignment 1 Individual Assignment: Mohammad Saad Uddin
INDIVIDUAL ASSIGNMENT
MATRIC
NAME MOHAMMAD SAAD UDDIN 012019090597
NO.
OBJECT ORIENTED
SUBJECT CODE CCS101303
PROGRAMMING
PROGRAMME DIT
DATE OF
8.10.2020
SUBMISSION
LECTURER’S SITI NAZAZIHAH BINTI RAHMAT
INSTRUCTION:
1
Question 1
2
CODE:
Demo1.java:
package demo1;
/**
* @author user
*/
3
/**
*/
System.out.println(r.computeSurfaceArea());
System.out.println(c.computeSurfaceArea());
Rectangle.java:
package demo1;
/**
* @author user
*/
double computeSurfaceArea()
surfaceArea = height*width;
return surfaceArea;
Cube.java:
package demo1;
4
/**
* @author user
*/
import java.util.Scanner;
double depth;
double computesurfaceArea()
System.out.println("Depth:");
depth = s.nextInt();
Result:
run:
36.0
36.0
(20 marks)
5
Question 2
Write a java program named Number whose main class hold two integer
variables. Assign any values to the variables. Use static to pass both variables
to methods named sum() and difference().
Define methods sum() and difference(); they compute the sum and
difference between the values of two arguments. Each method should perform
the computation and display result in method. Save file as Number.java.
6
7
b)
a)
CODE:
Number.java:
package number;
/**
* @author user
*/
import java.util.Scanner;
/**
*/
8
Scanner s = new Scanner(System.in);
System.out.println("First number");
System.out.println("Second number:");
sum.display();
d.display();
System.out.println("Product:"+p.Product(num1, num2));
Sum.java:
package number;
/**
* @author user
*/
int sum;
void display(){
9
System.out.println("Sum:" + sum);
Difference.java:
package number;
/**
* @author user
*/
int Difference;
void display(){
System.out.println("Difference:"+ Difference);
Product.java:
package number;
/**
* @author user
*/
10
{
Result:
run:
First number
Second number:
Sum:7
Difference:-1
Product:12
(20 marks)
11
Question 3
(4 marks)
1. Main method:
2. Variable:
(4 marks)
(c) Answer the question below using the given situation in Figure 1.
With the abstraction process you have to identify object, attributes
and methods in 1(i), 1(ii) and 1(iii).
12
Figure 1. Klinik Fatih
i. Object name
Patient registered
(2 marks)
ii. Attribute
(5 marks)
iii. Methods
(5 marks)
Question 4
(a) Understand the program code below in Figure 2 and answer the
following questions.
class shape {
protected int length, width;
void set (int a, int b){
length = a;
width = b; }
void show ()
{ System.out.println(length+” “+width); }}
class AreaRec{
public static void main(String [] args)
{ rectangle r= new rectangle();
r. Set(4,10);
r. Show();
r. SetArea();
13
r.Show();}}
Result:
0
40
(4 marks)
Subclass: rectangle
Superclass: shape
(4 marks)
package AreaRec;
/**
*
* @author user
*/
public class triangle extends shape
{
double area;
void setArea()
{ area= length*width*0.5;}
void Show ()
{
System.out.println(area); }
}
(6 marks)
14
iv. Identify TWO (2) data members and TWO (2) functions
members in superclass that maybe called in main function.
Data members: a, b
(6 marks)
Question 5
Write an apps named testingRoom that creates and object of each class, and
demonstrate all methods work correctly.
15
CODE:
TestingRoom1:
package testingroom1;
/**
* @author user
*/
import java.util.Scanner;
16
public class TestingRoom1 {
/**
*/
System.out.println("Name:");
System.out.println("Type of room:");
System.out.println("Number of nights:");
if ( roomType.equalsIgnoreCase( "normal")){
System.out.println(name);
h.displays();
else if ( roomType.equalsIgnoreCase("suite")){
System.out.println(name);
suite.display();
else {
System.out.println("Not available.");
17
}
hotel.java:
package testingroom1;
/**
* @author user
*/
int rn,n,r,p;
rate = 70;
else{
rate = 90;
rn = roomNum;
n = night;
r = rate;
p = price;
void displays(){
System.out.println("Price:"+ p);
suite.java:
18
package testingroom1;
/**
* @author user
*/
rn = roomNum;
n = night;
r = rate;
p = price;
void display(){
Result:
run:
Name:
SAAD
Type of room:
suite
Room number:
34
Number of nights:
19
SAAD
Price:220
(20 marks)
*******************************QUESTION END**************************************
20