You are on page 1of 20

ASSIGNMENT 1

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

This assignment is to achieve CO:


C01 Understand and design the fundamental concepts of object oriented
approach using UML and java. (C2,PLO1)
CO2 construct a Java program that requires the use of arrays to hold
primitive data types and objects. (C6,PLO2)
CO3 produce a GUI or applet using the GUI components that incorporates
event handlings (C3,PLO3)
CO4 Produce a Java application using the GUI components.  (C4,PLO3)
CO5 Develop a complete GUI based Java application or applet that
incorporates event- handlings. (C4,PLO3)

INSTRUCTION:

1. Date of Submission: 8.10.2020 (THURSDAY)


2. Cover page : STRIKING PINK
3. Submission should be done in class ONLY.
4. You must do every programming assignment by yourself. You are
permitted to get help ONLY from ME
5. A program will be considered wrong if it fails to work on one or more of
the test cases. It will otherwise be considered correct for the purposes
of this contest. {A program may still be wrong even if it passes all the
test input cases. Every effort will be made to design test cases that will
minimize the chance of missing an incorrect program.} A correct
program will be awarded the full points, an incorrect one zero!
6. Do not copy your friends work. You may have committed academic
misconduct which will leads you to get 0 for your assignment and no
remedial will be given.
7. Please refer to programming submission sample. Which, you need to
attach your clear to read source code and screenshot of output.

1
Question 1

As a programmer, you are appointed to create a class named Rectangle


that contains data fields for height, width and areaSurface, and a
method named computeAreaSurface(). Create a child class named
Cube. Cube contains an additional data field named depth, and a
computeAreaSurface() method that overrides parent method.

Write an application that instantiates a Rectangle object and a cube object


and displays the surface areas of the objects. Save the files as Cube.java,
Rectangle.java and Demo.java.

Formula: Area = width * height; and Cube= width*height*depth;

2
CODE:

Demo1.java:

package demo1;

/**

* @author user

*/

public class Demo1 {

3
/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

Rectangle r=new Rectangle();

Cube c=new Cube ();

System.out.println(r.computeSurfaceArea());

System.out.println(c.computeSurfaceArea());

Rectangle.java:

package demo1;

/**

* @author user

*/

public class Rectangle {

double surfaceArea, height = 4, width = 9;

double computeSurfaceArea()

surfaceArea = height*width;

return surfaceArea;

Cube.java:

package demo1;

4
/**

* @author user

*/

import java.util.Scanner;

public class Cube extends Rectangle {

double depth;

double computesurfaceArea()

Scanner s = new Scanner(System.in);

System.out.println("Depth:");

depth = s.nextInt();

return super.height* super.width * depth;

Result:

run:

36.0

36.0

BUILD SUCCESSFUL (total time: 0 seconds)

(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.

b. Add a new method named product()to the Number class. The


product() method should compute the multiplication product of two
integers. But not display the answer. Please return the answer to the calling
method. Save the application as Number2.java.

6
7
b)

a)

CODE:

Number.java:

package number;

/**

* @author user

*/

import java.util.Scanner;

public class Number {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

8
Scanner s = new Scanner(System.in);

System.out.println("First number");

int num1 = s.nextInt();

System.out.println("Second number:");

int num2 = s.nextInt();

Sum sum = new Sum(num1, num2);

Difference d = new Difference(num1, num2);

Product p = new Product();

sum.display();

d.display();

System.out.println("Product:"+p.Product(num1, num2));

Sum.java:

package number;

/**

* @author user

*/

public class Sum {

int sum;

Sum( int num1, int num2){

sum = num1 + num2;

void display(){

9
System.out.println("Sum:" + sum);

Difference.java:

package number;

/**

* @author user

*/

public class Difference {

int Difference;

Difference( int num1, int num2){

Difference = num1 - num2;

void display(){

System.out.println("Difference:"+ Difference);

Product.java:

package number;

/**

* @author user

*/

public class Product {

int Product( int num1, int num2)

10
{

return num1 * num2;

Result:

run:

First number

Second number:

Sum:7

Difference:-1

Product:12

BUILD SUCCESSFUL (total time: 11 seconds)

(20 marks)

11
Question 3

Object-oriented programming is a programming paradigm based on the


concept of "objects", which can contain data, in the form of fields, and code, in
the form of procedures. 

(a) Define inheritance.

One of the main principles of object-oriented programming languages is


inheritance. For a hierarchy of classes that share a collection of attributes and
methods, this is a process where you can derive a class from another class.

(4 marks)

(b) Explain TWO(2) anatomy of java program.

1. Main method:

There are several different types of classes in Java. A software


is a class with a key technique. The key technique is where the
execution of the programme begins and ends. While the main
method may ask for support from other classes , objects,
methods or parts of the Java language, the programme 's
overall execution starts and ends in the main method.

2. Variable:

A variable is a name or symbol which stands for a value. We


ask the machine to set aside a place in the memory where we
can store a certain piece of information when we declare a
variable. The software refers to the value stored there if you
use a variable 's name in your code.

(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).

Klinik Fatih Shah Alam is one of the new clinics launch on


January 2020. Once the patient registered, their information will
be recorded such as IC number, name of patient, address, date
and disease. The treatment will be done by the doctor incharge
on the current day. The appropriate medicine will be given to the
patient depend on the disease and the staff will print the patient
detail for the documenting.

12
Figure 1. Klinik Fatih

i. Object name

Patient registered

(2 marks)

ii. Attribute

IC number, name of patient, address, date and disease.

(5 marks)

iii. Methods

Klinik Fatih Shah Alam

(5 marks)

Question 4

Inheritance is a mechanism in which one class acquires the property of another


class. For example, a child inherits the traits of his/her parents.

(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 rectangle extends shape {


int area;
void setArea(){
area= length*width;}
void show ()
{ System.out.println(area); }}

class AreaRec{
public static void main(String [] args)
{ rectangle r= new rectangle();
r. Set(4,10);
r. Show();
r. SetArea();

13
r.Show();}}

Figure 2: Rectangle area

i. Identify the output of the program above.

Result:
0
40

(4 marks)

ii. Identify the subclass and superclass.

Subclass: rectangle

Superclass: shape

(4 marks)

iii. Create other class named Triangle that inherits the


properties from the superclass. Calculate the area of
Triangle in instance method and instantiates object for
Triangle class accordingly in main class.

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

Functions members: set

(6 marks)

Question 5

As a programmer, you have been asked to develop an application for Carlton


hotel. Create a class named Hotel that include an integer for the room number
and a double field for the nightly rental rate. Use get methods for these fields
and a constructor that requires an integer argument representing the room
number. The constructor sets the room rate based on the room number, room
numbered 299 and below are RM70 and other per night RM90.

Create an extended class named suite whose constructor requires a room


number and adds RM40 to the regular hotel room rate, which again based on
the room number.

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 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

Scanner s = new Scanner(System.in);

System.out.println("Name:");

String name = s.next();

System.out.println("Type of room:");

String roomType = s.next();

System.out.println(" Room number:");

int roomNum = s.nextInt();

System.out.println("Number of nights:");

int night = s.nextInt();

hotel h = new hotel( roomNum, night);

suite suite = new suite(roomNum, night);

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

*/

public class hotel {

int rn,n,r,p;

int rate, price;

hotel( int roomNum, int night){

if( roomNum < 300){

rate = 70;

else{

rate = 90;

price = rate * night;

rn = roomNum;

n = night;

r = rate;

p = price;

void displays(){

System.out.println("Price:"+ p);

suite.java:

18
package testingroom1;

/**

* @author user

*/

public class suite extends hotel {

public suite(int roomNum, int night){

super( roomNum, night);

rn = roomNum;

n = night;

r = rate;

price = (rate + 40) * night;

p = price;

void display(){

System.out.println(" Price:"+ p);

Result:

run:

Name:

SAAD

Type of room:

suite

Room number:

34

Number of nights:

19
SAAD

Price:220

BUILD SUCCESSFUL (total time: 31 seconds)

(20 marks)

*******************************QUESTION END**************************************

20

You might also like