[go: up one dir, main page]

0% found this document useful (0 votes)
1K views10 pages

Lab 1: Basic Concept of Classes: SHARON LAWAI (2021132273)

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

CSC584 Enterprise Programming

LAB 1: BASIC CONCEPT OF CLASSES


SHARON LAWAI (2021132273)

Prelab Activities
Answer the following questions in the space provided. Your answers should be concise
as possible.

1. What is an object?
• Object can be defined as entity that has behavior, state and identity.

2. What are the differences between private and public ?


• Private data members and methods are accessible only to instances of the class
while public data members and methods are accessible to everyone.

Question 3 and 4 are based on the Program 1.1

3. What is the class name for the above program statement?


• CalculateCharge

4. List down all data members for the above program statement.
• noBill
• price
• no_bill
• m
• y

MMR@FSKM2021
CSC584 Enterprise Programming

Lab Activities : Correct the Code


Determine if there is an error in each of the following program segments. If there is an error,
specify whether it is a logic error or a compilation error. Write the corrected code in the space
provided for each problem.

1. The following defines class Art, with no argument constructor that sets the art’s name to an
empty String and the price to 0.00 and a toArtString method that returns a String
containing the art’s name and its price.

• Compilation error. Java cannot run as there is no return data type in public
toArtString().

• import java.text.*;
public class Art {

private String name;


private double price;
private static DecimalFormat money = new DecimalFormat ("0.00");

public void Art(){


name = "";
price = 0.00;
}

public String toArtString(){


return name + " costs " + money.format(price);
}
}

MMR@FSKM2021
CSC584 Enterprise Programming

2. The following define two constructors for class Art.

• Compilation error. Both constructors have the same name and parameter. It is
redundant.
• public Art(double price) {
this.name = "This product";
this.price = price;
}

MMR@FSKM2021
CSC584 Enterprise Programming

Lab Exercise
Solve the problem given.

1. Type the following program in Program 1.2. Compile and Run.

• Input:

MMR@FSKM2021
CSC584 Enterprise Programming

• Output:

MMR@FSKM2021
CSC584 Enterprise Programming

2. Design a class named Triangle to represent triangle. The class contains:

a. Three double data fields named side1, side2 and side3 that specify the three sides of the
triangle. The default values are 1 for all the sides.

public class Triangle {


double side1;
double side2;
double side3;
}

b. Default constructor that creates a default triangle

public Triangle () {
side1 = 1;
side2 = 1;
side3 = 1;
}

c. A normal constructor that creates a triangle with the specified sides


public Triangle () {
side1 = s1;
side2 = s2;
side3 = s3;
}

d. The accessor and mutator methods for all the data member.
//accessor
public double getSide1() {
return side1;
}

public double getSide2() {


return side2;
}

public double getSide3() {


return side3;
}

MMR@FSKM2021
CSC584 Enterprise Programming

//mutator
public void setSide1 (double s1) {
Side1 = s1;
}

public void setSide2 (double s2) {


Side2 = s2;
}

public void setSide3 (double s3) {


Side3 = s3;
}

e. A method named calcArea() that returns the area of this triangle


public double calcArea () {
double area = side1 * side2;
return area;
}

f. A method named calcPerimeter() that returns the perimeter.


public double calcPerimeter (){
double perimeter = side1 + side2 + side3;
return perimeter;
}

Write a main program that creates two Triangle objects. Assign sides 4, 5 and 6 to the first
object and 1.5, 2.5 and 3.5 to the second object. Display the properties of both objects and find
their areas and perimeters.

public static void main (String [ ] args) {


Triangle object1 = new Triangle (4, 5, 6);
Triangle object2 = new Triangle (1.5, 2.5, 3.5);

System.out.println(object1.toString() + object1.calcArea() + object1.calcPerimeter());


System.out.println(object2.toString() + object2.calcArea() + object2.calcPerimeter());
}

MMR@FSKM2021
CSC584 Enterprise Programming

Postlab Exercise

Consider the following declaration of a Student class in Program 1.3

public class Student


{ private String studentName;
private String student ID;
private String program;// e.g. CS233
private double test;
private double assignment;
private double final;

}
Program 1.3

Write the following to complete the Student class


a. A default constructor
public Student () {
studentName = null;
studentID = null;
program = null;
test = 0;
assignment = 0;
final = 0;
}

b. The mutator and accessor methods for all data members


//mutator
public void setStudentName (String stuName) {
studentName = stuName;
}

public void setStudentID (String stuID) {


studentID = stuID;
}

public void setProgram (String prog) {


program = prog;
}

MMR@FSKM2021
CSC584 Enterprise Programming

public void setTest (double tst) {


test = tst;
}
public void setAssignment (double assign) {
assignment = assign;
}
public void setFinal (double fnl) {
final = fnl;
}

//Accessor
public double getStudentName () {
return studentName;
}

public double getStudentID () {


return studentID;
}

public double getProgram () {


return program;
}

public double getTest () {


return test;
}

public double getAssignment () {


return assignment;
}

public double getFinal () {


return final;
}

MMR@FSKM2021
CSC584 Enterprise Programming

c. A toString method that returns a String containing all data members.


public String toString ( ){
System.out.println(“Student Name: “ + studentName + “\nStudent ID: “ + studentID +
“\nProgram: “ + program + “\nTest: “ + test + “\nAssignment: “ + assignment +
“\nFinal: “ + final);
}

d. A processor method named calculateFinalMarks(). This method will calculate and return
final marks of student based on the following formula.

Final marks = test (30%) + assignment (20%) + final (50%)

public double calculateFinalMarks ( ){

double finalMarks = (getTest() * 0.3) + (getAssignment() * 0.2) + (getFinal() * 0.5);

return finalMarks;

e. Write a main method that create four objects of student and assigned each of them with
values for each data members. Then display the student name, student ID and final
marks for all the students objects
public static void main (String [ ] args) {
Student stu1 = new Student (“Sharon”, “2021132273”, “CS240”, 80, 90, 95);
Student stu2 = new Student (“David”, “2021132274”, “CS230”, 81, 91, 96);
Student stu3 = new Student (“Siti”, “2021132275”, “CS110”, 70, 80, 85);
Student stu4 = new Student (“Arnold“, “2021132276”, “AS110”, 70, 80, 85);
}

MMR@FSKM2021

You might also like