0% found this document useful (0 votes)
25 views11 pages

Lab Oop

The document describes two classes - Program and Land. The Program class models a university program with attributes like code, description, duration etc. The Land class models a land property with attributes like owner ID, name, type and area. Both classes have default, normal and copy constructors along with getter/setter methods. The main method creates objects, takes input and calls methods to output property tax and description.

Uploaded by

Khairil Hazmie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views11 pages

Lab Oop

The document describes two classes - Program and Land. The Program class models a university program with attributes like code, description, duration etc. The Land class models a land property with attributes like owner ID, name, type and area. Both classes have default, normal and copy constructors along with getter/setter methods. The main method creates objects, takes input and calls methods to output property tax and description.

Uploaded by

Khairil Hazmie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

College of Computing Informatics & Media

Bachelor of Computer Science (Hons.) Netcentric Computing

CSC435

Object Oriented Programming

Task: Lab Assignment 2

Name Khairil Hazmie Bin Abdul Razak


Student ID 2022697564
Group RCDCS2513A
Lecturer Sir Nizam Bin Osman
• Program class
public class Program
{

//Data Members
private String Prog_Code;
private String Prog_Desc;
private int Prog_duration;
private String Faculty;
private String Prog_Head;

//Method members
//Default Constructor
public Program()
{
Prog_Code = "";
Prog_Desc = "";
Prog_duration = 0;
Faculty = "";
Prog_Head = "";
}

//Normal Constructor
public Program(String PC, String PDesc, int Pdura, String Fc,
String PH)
{
Prog_Code = PC;
Prog_Desc = PDesc;
Prog_duration = Pdura;
Faculty = Fc;
Prog_Head = PH;
}

//Copy Constructor
public Program(Program p)
{
Prog_Code = p.Prog_Code;
Prog_Desc = p.Prog_Desc;
Prog_duration = p.Prog_duration;
Faculty = p.Faculty;
Prog_Head = p.Prog_Head;
}

//Mutator/Setter method
public void setProg_Code(String PC)
{
Prog_Code = PC;
}
public void setProg_Desc(String PDesc)
{
Prog_Desc = PDesc;
}

public void setProg_duration(int Pdura)


{
Prog_duration = Pdura;
}

public void setFaculty(String Fc)


{
Faculty = Fc;
}

public void setProg_Head(String PH)


{
Prog_Head = PH;
}

//Retriever
public String getProg_Code()
{
return Prog_Code;
}

public String getProg_Desc()


{
return Prog_Desc;
}

public int getProg_duration()


{
return Prog_duration;
}

public String getFaculty()


{
return Faculty;
}

public String getProg_Head()


{
return Prog_Head;
}
//Processor
public String DetermineProgramLevel()
{
if (Prog_Code != null && Prog_Code.length() >= 3)
{
char thirdChar = Prog_Code.charAt(2);
switch (thirdChar)
{
case '0':
return "Certificate";
case '1':
return "Diploma";
case '2':
return "Degree";
case '7':
return "Master";
case '9':
return "Doctorate";

default:
return "Unknown Program Level";
}
}
else
{
return "Invalid Program Code";
}
}

//Printer
public String toString()
{
return "\n\nProgram code: " + Prog_Code + "\nProgram
Description: " + Prog_Desc + "\nProgram Duration: " + Prog_duration +
"\nFaculty: " + Faculty + "\nProgram Header: " + Prog_Head;
}
}
• ProgramApp main
import java.util.*;

public class ProgramApp


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

//Step 1 Create an object


Program prog = new Program();

//Step 2 Input
//Setter

System.out.print("Enter program code: ");


String programCode = scan.nextLine();
prog.setProg_Code(programCode);

System.out.print("Enter program description: ");


String programDesc = scan.nextLine();
prog.setProg_Desc(programDesc);

System.out.print("Enter program duration: ");


int programDuration = scan.nextInt();
prog.setProg_duration(programDuration);

scan.nextLine();

System.out.print("Enter Faculty: ");


String faculty = scan.nextLine();
prog.setFaculty(faculty);

System.out.print("Enter program head: ");


String programHead = scan.nextLine();
prog.setProg_Head(programHead);

//normal Constructor

String Prog_Level = prog.DetermineProgramLevel();

System.out.println(prog.toString() + "\nProgram Level: " +


Prog_Level);
}
}
• Input & Output

Figure 1: Example of input and ouput 1.

Figure 2: Example of input and output 2.


• Land Class
public class Land
{

//Data Members
private int Id;
private String Owner_Name;
private char house_type;
private double area;

//Method members

//Default Constructor
public Land()
{
Id = 0;
Owner_Name = "";
house_type = '\0';
area = 0;
}

//Normal Constructor
public Land(int ID, String ON, char HT, double A)
{
Id = ID;
Owner_Name = ON;
house_type = HT;
area = A;
}

//Copy Constructor
public Land(Land l)
{
Id = l.Id;
Owner_Name = l.Owner_Name;
house_type = l.house_type;
area = l.area;
}

//Mutator/Setter method
public void setId(int ID)
{
Id = ID;
}

public void setOwner_Name(String ON)


{
Owner_Name = ON;
}
public void sethouse_type(char HT)
{
house_type = HT;
}

public void setarea(double A)


{
area = A;
}

//Getter

public int getId()


{
return Id;
}

public String getOwner_Name()


{
return Owner_Name;
}

public char gethouse_type()


{
return house_type;
}

public double getarea()


{
return area;
}

//Processor
public double CalcTax()
{
double tax = 0;
if (house_type == 'T') {
tax = area * 10;
}
else if (house_type == 'S') {
tax = area * 15;
}
else if (house_type == 'B') {
tax = area * 20;
}
else if (house_type == 'C') {
tax = area * 30;
}
return tax;
}
public String HouseDescription()
{
String Description = "";

if (house_type == 'T') {
Description = "Terrace";
}
else if (house_type == 'S') {
Description = "Semi-Detached";
}
else if (house_type == 'B') {
Description = "Bungalow";
}
else if (house_type == 'C') {
Description = "Condominium";
}

return Description;
}

//Printer
public String toString()
{
return "\n\nUser id: " + Id + "\nOwner name: " + Owner_Name;
}
}
• LandApp main
import java.util.*;

public class LandApp


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

//Step 1 Create an object


Land land = new Land();

//Step 2 Input
//Setter

System.out.print("Enter User Id: ");


int UserId = scan.nextInt();
land.setId(UserId);

scan.nextLine();

System.out.print("Enter owner name: ");


String Name = scan.nextLine();
land.setOwner_Name(Name);

System.out.print("Enter house type: ");


char Type = scan.nextLine().charAt(0);
land.sethouse_type(Type);

System.out.print("Enter Area: ");


double AREA = scan.nextDouble();
land.setarea(AREA);

//normal Constructor

double totalTax = land.CalcTax();


String houseType = land.HouseDescription();

System.out.println(land.toString() + "\nHouse type: " +


houseType + "\nTotal tax: " + totalTax);
}
}
• Input & Output

Figure 3:Example of input and output 1.

Figure 4:Example of input and output 2.

You might also like