Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

JOSE RIZAL UNIVERSITY

CSC33 C51G USING JAVA


JAVA CLASSES & OBJECTS SAMPLE PROGRAMS
// Creating your own classes:
// Filename: Car.java
// ****************************************************************************************
public class Car
{
private String plateNumber;
private String color;
private static int countCar;
private double price;
private static double totalPrice;
public Car()
{
plateNumber = "XXX-369";
color = "White";
price = 100;
}
public Car(String plateNum, String carColor, double carPrice)
{
plateNumber = plateNum;
color = carColor;
price = carPrice;
}
public String getPlateNumber()
{
countCar++;
return plateNumber;
}
public void setPlateNumber(String temp)
{
plateNumber = temp;
}
public String getColor()
{
return color;
}
public void setColor(String temp)
{
color = temp;
}

Prepared by: Mr. Roel Richard C. Traballo

JOSE RIZAL UNIVERSITY


CSC33 C51G USING JAVA
public static int getCountCar()
{
return countCar;
}
public double getPrice()
{
totalPrice = totalPrice + price;
return price;
}
public void setPrice(double temp)
{
price = temp;
}
public static double getTotalPrice()
{
return totalPrice;
}
}
// ****************************************************************************************
// Filename: SubCar.java
// ****************************************************************************************
public class SubCar
{
public static void main(String args[])
{
Car carA = new Car();
Car carB = new Car();
Car carC = new Car();
Car carD = new Car("MMM-777","Black",200);
System.out.println("Plate Number\tColor\t\tPrice");
System.out.println(carA.getPlateNumber()+"\t\t"+carA.getColor()+"\t\t"+carA.getPrice());
carB.setPlateNumber("ABC-123");carB.setColor("Blue");carB.setPrice(500);
System.out.println(carB.getPlateNumber()+"\t\t"+carB.getColor()+"\t\t"+carB.getPrice());
carC.setPlateNumber("XYZ-987");carC.setColor("Red");carC.setPrice(700);
System.out.println(carC.getPlateNumber()+"\t\t"+carC.getColor()+"\t\t"+carC.getPrice());
System.out.println(carD.getPlateNumber()+"\t\t"+carD.getColor()+"\t\t"+carD.getPrice());
System.out.println("Total Car: " + Car.getCountCar());
System.out.println("Total Price: " + Car.getTotalPrice());
}
}
// ****************************************************************************************

Prepared by: Mr. Roel Richard C. Traballo

JOSE RIZAL UNIVERSITY


CSC33 C51G USING JAVA
// Creating your own classes: Sample #1:
// Filename: ClassDeclaration.java
// ****************************************************************************************
public class ClassDeclaration{
private String name;
private String course;
private double tfee;
private static double total;
public void setName(String temp){
this.name = temp;
}
public String getName(){
return name;
}
public void setCourse(String temp){
course = temp;
}
public String getCourse(){
return course;
}
public void setTfee(double temp){
tfee = temp;
}
public double getTfee(){
total+=tfee;
return tfee;
}
public static double computeTotal(){
return total;
}
}
// ****************************************************************************************
// Filename: SubClassExample_1.java
// ****************************************************************************************
public class SubClassExample_1
{
public static void main(String args[]) throws Exception
{
ClassDeclaration cd1 = new ClassDeclaration();
ClassDeclaration cd2 = new ClassDeclaration();
ClassDeclaration cd3 = new ClassDeclaration();
cd1.setName("Darrel");
cd2.setName("Ayien");
cd3.setName("Raine");
Prepared by: Mr. Roel Richard C. Traballo

cd1.setCourse("BSCS");
cd2.setCourse("BSIT");
cd3.setCourse("BSN");

cd1.setTfee(1000);
cd2.setTfee(2500);
cd3.setTfee(5000);

JOSE RIZAL UNIVERSITY


CSC33 C51G USING JAVA
System.out.println("NAME\tCOURSE\tTUITION FEE");
System.out.println(cd1.getName()+"\t"+cd1.getCourse()+"\t"+cd1.getTfee());
System.out.println(cd2.getName()+"\t"+cd2.getCourse()+"\t"+cd2.getTfee());
System.out.println(cd3.getName()+"\t"+cd3.getCourse()+"\t"+cd3.getTfee());
System.out.println("\nTotal Fees: " + ClassDeclaration.computeTotal());
}
}
// ****************************************************************************************
// Modified ClassDeclaration class with Constructor
// Filename: ClassDeclaration.java
// ****************************************************************************************
public class ClassDeclaration{
private String name;
private String course;
private double tfee;
private static double total;
public ClassDeclaration(){
this.name = "Carings";
this.course = "BSCS";
this.tfee = 1000;
}
public void setName(String temp){
this.name = temp;
}
public String getName(){
return name;
}
public void setCourse(String temp){
course = temp;
}
public String getCourse(){
return course;
}
public void setTfee(double temp){
tfee = temp;
}
public double getTfee(){
total+=tfee;
return tfee;
}
public static double computeTotal(){
return total;
}
}
// ****************************************************************************************
Prepared by: Mr. Roel Richard C. Traballo

JOSE RIZAL UNIVERSITY


CSC33 C51G USING JAVA
// Using the same class ClassDeclaration
// Filename: SubClassExample_2.java
// ****************************************************************************************
public class SubClassExample_2
{
public static void main(String args[]) throws Exception
{
ClassDeclaration cd = new ClassDeclaration();
System.out.println("NAME\tCOURSE\tTUITION FEE");
System.out.println(cd.getName()+"\t"+cd.getCourse()+"\t"+cd.getTfee());
System.out.println("\nTotal Fees: " + ClassDeclaration.computeTotal());
}
}
// ****************************************************************************************
// Modified ClassDeclaration class with Constructor Overloading
// Filename: ClassDeclaration.java
// ****************************************************************************************
public class ClassDeclaration{
private String name;
private String course;
private double tfee;
private static double total;
public ClassDeclaration(){
this.name = "Darrel";
this.course = "BSCS";
this.tfee = 1000;
}
public ClassDeclaration(String neym, String cors, double fee){
this.name = neym;
this.course = cors;
this.tfee = fee;
}
public void setName(String temp){
this.name = temp;
}
public String getName(){
return name;
}
public void setCourse(String temp){
course = temp;
}
public String getCourse(){
return course;
}
Prepared by: Mr. Roel Richard C. Traballo

JOSE RIZAL UNIVERSITY


CSC33 C51G USING JAVA
public void setTfee(double temp){
tfee = temp;
}
public double getTfee(){
total+=tfee;
return tfee;
}
public static double computeTotal(){
return total;
}
}
// ****************************************************************************************
// Using the same class ClassDeclaration
// Filename: SubClassExample_3.java
// ****************************************************************************************
public class SubClassExample_3
{
public static void main(String args[]) throws Exception
{
ClassDeclaration cd1 = new ClassDeclaration();
ClassDeclaration cd2 = new ClassDeclaration("Ayien","BSIT",2500);
ClassDeclaration cd3 = new ClassDeclaration("Raine","BSN",5000);
System.out.println("NAME\tCOURSE\tTUITION FEE");
System.out.println(cd1.getName()+"\t"+cd1.getCourse()+"\t"+cd1.getTfee());
System.out.println(cd2.getName()+"\t"+cd2.getCourse()+"\t"+cd2.getTfee());
System.out.println(cd3.getName()+"\t"+cd3.getCourse()+"\t"+cd3.getTfee());
System.out.println("\nTotal Fees: " + ClassDeclaration.computeTotal());
}
}
// ****************************************************************************************
// Using the same class ClassDeclaration
// Filename: SubClassExample_4.java
// ****************************************************************************************
public class SubClassExample_4
{
public static void main(String args[]) throws Exception
{
ClassDeclaration cd = new ClassDeclaration();
String name[] = {"Darrel","Ayien","Raine"};
String course[] = {"BSCS","BSIT","BSN"};
double tfee[] = {1000,2500,5000};
Prepared by: Mr. Roel Richard C. Traballo

JOSE RIZAL UNIVERSITY


CSC33 C51G USING JAVA
System.out.println("NAME\tCOURSE\tTUITION FEE");
for(int i=0; i<3; i++)
{
cd.setName(name[i]); cd.setCourse(course[i]);
cd.setTfee(tfee[i]);
System.out.println(cd.getName()+"\t"+cd.getCourse()+"\t"+cd.getTfee());
}
System.out.println("\nTotal Fees: " + ClassDeclaration.computeTotal());
}
}
// ****************************************************************************************
// Using the same class ClassDeclaration
// Filename: SubClassExample_5.java
// ****************************************************************************************
import java.io.*;
public class SubClassExample_5
{
public static void main(String args[]) throws Exception
{
ClassDeclaration cd = new ClassDeclaration();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name, course;
double tfee;
System.out.print("Enter name: ");
name = br.readLine();
System.out.print("Enter course: ");
course = br.readLine();
System.out.print("Enter tuition fee: ");
tfee = Double.parseDouble(br.readLine());
cd.setName(name);

cd.setCourse(course); cd.setTfee(tfee);

System.out.println("\n\nNAME\tCOURSE\tTUITION FEE");
System.out.println(cd.getName()+"\t"+cd.getCourse()+"\t"+cd.getTfee());
System.out.println("\nTotal Fees: " + ClassDeclaration.computeTotal());
}
}
// ****************************************************************************************

Prepared by: Mr. Roel Richard C. Traballo

You might also like