0% found this document useful (0 votes)
9 views

Lab - Exp - 5 (Creating Class Diagrams) - 1

Uploaded by

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

Lab - Exp - 5 (Creating Class Diagrams) - 1

Uploaded by

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

COLLEGE OF COMPUTER STUDEIS

INFORMATION TECHNOLOGY DEPARTMENT

CCS0023L
(Object Oriented Programming)

EXERCISE

5
Creating Classes and Objects

Student Name / Group Ralph Andre Garcia


Name:
Name Role
Members (if Group):

Section:
DW21
Professor:
Tio, Christopher Rene

I. PROGRAM OUTCOME/S (PO) ADRESSED BY THE LABORATORY EXERCISE


a. Apply knowledge of computing appropriate to the discipline.

II. COURSE LEARNING OUTCOME/S (CLO) ADDDRESSED BY LABORATORY


EXERCISE
2 Apply knowledge of computing fundamentals and develop computer programs as a result of
evaluating possible alternative program constructs that will address the need for automating
real life tasks, taking into account the efficiency and effects of their choice of constraints.

III. INTENDED LEARNING OUTOME/S (ILO) ADDRESSES BY THE LABORATORY


EXERCISE
 Create Java programs with classes and objects

IV. BACKGROUND INFORMATION

A class is a considered as the center of Object Oriented Programming methodology and


defines the abstract characteristics of a thing (object), including the thing's characteristics (its
attributes, fields or properties) and the thing's behaviors (the things it can do, or methods,
operations or features). An object is an instantiation of a class.

In a way, a class and its objects have the relationship of a data type and variables. A class is
simply a template for holding objects. It is abstract but objects are real.
Classes are generally declared using the keyword “class”.
V. EXPERIMENTAL PROCEDURE: Create the Class Diagram of the following program
description using JDeveloper.

1. A zoo contains a large number of different types of animal. All animals respond to
a message 'talk()' by announcing what they are and their name and age. Each type
of animal is represented by a different sub-class of the animal class.
The zoo itself provides methods to add and subtract animals and responds to the
message 'feedingTime()' by sending the message talk to all the animals.
Animal classes
Create an Animal class and appropriate sub-classes for say Lion, Tiger etc.
Zoo class
Create a Zoo class can contain a number of animals and provides methods to add
and remove animals as well as responding to the method 'feedingTime()'.

public class Animals


{
public int age;
public String name;
public String Lion;
public String Tiger;
public String Tarsier;
public String PEagle;
public String Hippo;

public Animals(){
this.Lion="";
this.Tiger="";
this.Tarsier="";
this.PEagle="";
this.Hippo="";
}
public void setLion(String Lion){
this.Lion = Lion;
}
public String getLion(){
return this.Lion;
}

public void setTiger(String Tiger){


this.Tiger = Tiger;
}
public String getTiger(){
return this.Tiger;
}

public void setTarsier(String Tarsier){


this.Tarsier = Tarsier;
}
public String getTarsier(){
return this.Tarsier;
}

public void setPEagle(String PEagle){


this.PEagle = PEagle;
}
public String getPEagle(){
return this.PEagle;
}

public void setHippo(String Hippo){


this.Hippo = Hippo;
}
public String getHippo(){
return this.Hippo;
}

public class Zoo{


public int TotalAnimals= 5;
public int age;
public String name;

public class Animals {


public String name;
public int age;

public void Setname(String name){


this.name = name;
}

public String getName(){


return this.name;
}
public void setAge (int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void vocalize() {
System.out.println("Animal");
}
}

public class Lion extends Animals {


public void vocalize() {
System.out.println("Lion");
}
}

public Lion lion = new Lion();

public class Tiger extends Animals {


public void vocalize() {
System.out.println("Tiger");
}
}

public Tiger tiger = new Tiger();

public class Tarsier extends Animals {


public void vocalize() {
System.out.println("Tarsier");
}
}

public Tarsier tarsier = new Tarsier();

public class PEagle extends Animals {


public void vocalize() {
System.out.println("PEagle");
}
}

public PEagle peagle = new PEagle();

public class Hippo extends Animals {


public void vocalize() {
System.out.println("Hippo");
}
}

public Hippo hippo = new Hippo();


}

public class Application {


public static void main(String [] args){
System.out.println("Lion");
System.out.println("Name: Mufasa");
System.out.println("Age: 8\n");
System.out.println("Tiger");
System.out.println("Name: Xiao");
System.out.println("Age: 11\n");
System.out.println("Tarsier");
System.out.println("Name: Red");
System.out.println("Age: 5\n");
System.out.println("Eagle");
System.out.println("Name: Juan");
System.out.println("Age: 45\n");
System.out.println("Hippo");
System.out.println("Name: Moo deng");
System.out.println("Age: 1\n");

}
}
Menu class
As this application, like so many, will require a menu for the user to drive it, create
a Menu class which has a constructor which enables a set of menu item strings to
be specified and a method which displays the menu and returns the user choice as
an integer. This method should perform some error checking on the user input.
Application class
Create a suitable application class with a main method which provides a menu for
user interaction and interacts with a zoo object.

2. Write a grading program for each class with the following grading policies:

a. There are two quizzes, each graded on the basis of 10 points.

b. There is one midterm exam and one final exam, each graded on the basis of 100
points.

c. The final exam counts for 50% of the grade, the midterm counts for 25%, and
the two quizzes together count for a total of 25%.(Do not forget to normalize the
quiz scores. They should be converted to a percent before they are average in.) Any
grade of 90 or more is an A, any grade of 80 or more (but less than 90 is B, any
grade of 70 or more (but less than 80) is a C, any grade of 60 or more (but less than
70) is a D, and any grade below 60 is an F. The program will read in the student’s
scores and output the student’s record, which consists of two quiz and two exam
scores as well as the student’s overall numeric score for the entire course and final
letter grade. Define and use a class for the student record. The class should have
instance variables for the quizzes, midterm, final, course overall numeric score, and
course final letter grade. The overall numeric score is a number in the range 0 to
100, which represents the weighted average of the student’s work. The class should
have input and output methods. The input method should not ask for the final
numeric grade nor should it ask for the final letter grade. The class should have
methods to compute the overall numeric grade and the final letter grade. These last
two methods will be void methods that set the appropriate instance variables.
Remember, one method can call another method. If you prefer, you can define a
single method that sets both the overall numeric score and the
final letter grade, but if you do this, use a helping method. Your program should
use all the methods we discussed. Your class should have a reasonable set of
accessor and mutator methods, whether or not your program uses them. You may
add other methods if you wish.

public class Menu {


private String[] menuItems;
public Menu(String[] menuItems) {
this.menuItems = menuItems;
}

public int displayMenu() {


while (true) {
for (int i = 0; i < menuItems.length; i++) {
System.out.println((i + 1) + ". " + menuItems[i]);
}
System.out.print("Enter your choice (1-" + menuItems.length + "): ");
try {
int choice = Integer.parseInt(System.console().readLine());
if (choice >= 1 && choice <= menuItems.length) {
return choice;
} else {
System.out.println("Invalid choice. Please try again.");
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a number.");
}
}
}
}

public class Application {


public static void main(String[] args) {

String[] menuItems = {"Option 1", "Option 2", "Option 3"};


Menu menu = new Menu(menuItems);

while (true) {
int choice = menu.displayMenu();

}
}
}

public class StudentRecord {


private double quiz1;
private double quiz2;
private double midterm;
private double finalExam;
private double overallNumericScore;
private char finalLetterGrade;

public StudentRecord() {
this.quiz1 = 0;
this.quiz2 = 0;
this.midterm = 0;
this.finalExam = 0;
this.overallNumericScore = 0;
this.finalLetterGrade = ' ';
}

public void inputScores() {


System.out.print("Enter quiz 1 score (out of 10): ");
this.quiz1 = Double.parseDouble(System.console().readLine());
System.out.print("Enter quiz 2 score (out of 10): ");
this.quiz2 = Double.parseDouble(System.console().readLine());
System.out.print("Enter midterm score (out of 100): ");
this.midterm = Double.parseDouble(System.console().readLine());
System.out.print("Enter final exam score (out of 100): ");
this.finalExam = Double.parseDouble(System.console().readLine());
}

public void computeOverallNumericScore() {


double quizAverage = (this.quiz1 + this.quiz2) / 2;
quizAverage = (quizAverage / 10) * 25; // Normalize quiz scores
this.overallNumericScore = (quizAverage + (this.midterm * 0.25) + (this.finalExam * 0.5));
}

public void computeFinalLetterGrade() {


if (this.overallNumericScore >= 90) {
this.finalLetterGrade = 'A';
} else if (this.overallNumericScore >= 80) {
this.finalLetterGrade = 'B';
} else if (this.overallNumericScore >= 70) {
this.finalLetterGrade = 'C';
} else if (this.overallNumericScore >= 60) {
this.finalLetterGrade = 'D';
} else {
this.finalLetterGrade = 'F';
}
}

public void outputRecord() {


System.out.println("Quiz 1 score: " + this.quiz1);
System.out.println("Quiz 2 score: " + this.quiz2);
System.out.println("Midterm score: " + this.midterm);
System.out.println("Final exam score: " + this.finalExam );
System.out.println("Overall numeric score: " + this.overallNumericScore);
System.out.println("Final letter grade: " + this.finalLetterGrade);
}
}

public class GradingProgram {


public static void main(String[] args) {
StudentRecord studentRecord = new StudentRecord();
studentRecord.inputScores();
studentRecord.computeOverallNumericScore();
studentRecord.computeFinalLetterGrade();
studentRecord.outputRecord();
}
}
3. Create a class that graphs the grade distribution (number of A’s, B’s, C’s, D’s, and
F’s) horizontally by printing lines with proportionate numbers of asterisks
corresponding to the percentage of grades in each category. Write methods to set the
number of each letter grade; red the number of each letter grade, return the total
number of grades, return the percent of each letter grade as a whole number between 0
and 100, inclusive; and draw the graph. Set it up so that 50 asterisks correspond to
100% (each one corresponds 2%), include a scale on the horizontal axis indicating
each 10% increment from 0 to 100%, and label each line with its letter grade. For
example, if there are 1 A’s, 4 B’s, 6 C’s, 2 D’s and 1 F, the total number of grades is
14, the percentage of A’s is 7, percentage of B’s is 29, percentage of C’s is 43,
percentage of D’s is 14, and percentage of F’s is 7. The A row would contain 4
asterisks (7% of 50 rounded to the nearest the B row 14, the C row 21, the D row 7,
and the F row 4, so the graph would look like this

0 10 20 30 40 50 60 70 80 90 100
***************************************************
**** A
************** B
********************* C
******* D
**** F

VI. QUESTION AND ANSWER:

1. What is the difference between classes and objects?


Class is a detailed description, the definition, and the template of what an object will be. But it is
not the object itself. Also, what we call, a class is the building block that leads to Object-
Oriented Programming. Object is an instance of a class. All data members and member functions
of the class can be accessed with the help of objects. When a class is defined, no memory is
allocated, but memory is allocated when it is instantiated (i.e. an object is created) According to
geeksforgeeks.

2. Consider the following class:


public class IdentifyMyParts {
public static int x = 7;
public int y = 3;
}

1. What are the class variables?


X is the class variables
2. What are the instance variables?
Y is the instance variable.
Note: The following rubrics/metrics will be used to grade students’ output in the lab
Exercise 5.

Program (100 pts.) (Excellent) (Good) (Fair) (Poor)


Program Program executes Program executes Program executes Program does not
execution (20pts) correctly with no with less than 3 with more than 3 execute (10-11pts)
syntax or runtime errors (15-17pts) errors (12-14pts)
errors (18-20pts)
Correct output Program displays Output has minor Output has Output is incorrect
(20pts) correct output errors (15-17pts) multiple errors (10-11pts)
with no errors (12-14pts)
(18-20pts)
Design of output Program displays Program displays Program does not Output is poorly
(10pts) more than minimally display the designed (5pts)
expected (10pts) expected output required output (6-
(8-9pts) 7pts)
Design of logic Program is Program has slight Program has Program is
(20pts) logically well logic errors that do significant logic incorrect (10-
designed (18- no significantly errors (3-5pts) 11pts)
20pts) affect the results
(15-17pts)
Standards Program code is Few inappropriate Several Program is poorly
(20pts) stylistically well design choices inappropriate written (10-11pts)
designed (18- (i.e. poor variable design choices
20pts) names, improper (i.e. poor variable
indentation) (15- names, improper
17pts) indentation) (12-
14pts)
Delivery The program was The program was The program was The program was
(10pts) delivered on time. delivered a day delivered two days delivered more
(10pts) after the deadline. after the deadline. than two days after
(8-9pts) (6-7pts) the deadline. (5pts)

Topic Introduction to Object-


Oriented Programming
Lab Activity No 5a
Lab Activity Zoo Animals
CLO 2
Program execution (20)
Correct output (20)
Design of output (10)
Design of logic (20)
Standards (20)
Delivery (10)
TOTAL

Topic Introduction to Object-


Oriented Programming
Lab Activity No 5b
Lab Activity Grading System
CLO 2
Program execution (20)
Correct output (20)
Design of output (10)
Design of logic (20)
Standards (20)
Delivery (10)
TOTAL

Topic Introduction to Object-


Oriented Programming
Lab Activity No 5c
Lab Activity Grade Distribution
CLO 2
Program execution (20)
Correct output (20)
Design of output (10)
Design of logic (20)
Standards (20)
Delivery (10)
TOTAL

You might also like