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

Ex OOP Revisited Coding

The document defines a Clothes class with properties and methods like getters, setters, and calculating discounted price. It then provides two examples of using the class by prompting user input to populate a Clothes array, calculating values, and displaying output.

Uploaded by

tulyptulyp
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)
14 views

Ex OOP Revisited Coding

The document defines a Clothes class with properties and methods like getters, setters, and calculating discounted price. It then provides two examples of using the class by prompting user input to populate a Clothes array, calculating values, and displaying output.

Uploaded by

tulyptulyp
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/ 5

Example(OOP Revisit)-Coding

Clothes Class Definition:

public class Clothes


{ private String size;
private double price;
private double discPercent; //e.g:0.05 represent 5%

public Clothes()//default constructor


{ size=" ";
price=0.00;
discPercent=0.00;
}

//normal constructor
public Clothes(String size,double price,double percent)
{ this.size=size;
this.price=price;
discPercent=percent;
}

//setter/mutator method
public void setClothes(String s,double p,double percent)
{ this.size=s;
price=p;
discPercent=percent;
}

//calcNewPrice() processor method


public double calcNewPrice()
{ double newPrice;
newPrice=price-(price*discPercent);
return newPrice;
}

//retriever/accessor method
public String getSize(){return size;}
//retriever/accessor method
public double getPrice(){return price;}
//retriever/accessor method
public double getDiscPercent(){return discPercent;}
//display/retriever method
public String toString()
{return ("\nSize :"+size+"\tPrice:"+price+"\tPercent Discount:"+discPercent);}
//display/retriever method
public void display()
{ System.out.println("\nSize :"+size+"\tPrice:"+price+"\tPercent Discount:"+discPercent);}

}
clothApp1 coding

/*Write a main method that will:


Declare an array to store 3 clothes information.
Prompt the user to input a data and store into an array.
Calculate the price after discount .
Display the average value of normal price.
Display the detail of each cloth including price after discount.
*you must use default constructor and toString() method
*/
import java.util.*;
public class clothApp1
{
public static void main(String args[])
{
Clothes cloth[]=new Clothes[3];//declare an array to store 3 clothes information

String sz;
double price,disc;
Scanner input=new Scanner(System.in);
Scanner inputText=new Scanner(System.in);

for(int i=0;i<3;i++)
{ //input data
System.out.println("\nCloth "+(i+1)+":");
System.out.println("Enter Size:");
sz=inputText.next();
System.out.println("Enter Price:");
price=input.nextDouble();
System.out.println("Enter Percent Discount(Eg:0.05 represent 5%):");
disc=input.nextDouble();
cloth[i]=new Clothes(); //call default constructor
cloth[i].setClothes(sz,price,disc); //set/send the value into an array
}

double nPrice[]=new double[3];

for(int i=0;i<3;i++)
{ //Calculate the price after discount for each cloth
nPrice[i]=cloth[i].calcNewPrice();
}

//Display the average value of normal price


double sum=0,avg;
for(int i=0;i<3;i++)
{ sum=sum+cloth[i].getPrice();} //sum=sum+price;//it is WRONG!!
avg=sum/3;
System.out.println("Average value for normal price:"+avg);

//Display the detail of each cloth(using toString() including price after discount)
System.out.println("\nDetail for each cloth:");
for(int i=0;i<3;i++)
{ System.out.println("\nCloth "+(i+1)+":");
System.out.println(cloth[i].toString()+"Price after discount:"+nPrice[i]);
//cloth[i].toString(); this is WRONG
}

}
}
clothApp2 coding:

/*Write a main method that will:


Declare an array to store 3 clothes information.
Prompt the user to input a data and store into an array.
Calculate the price after discount .
Display the average value of normal price.
Display the detail of each cloth including price after discount.
*you must use normal constructor and display() method
*/
import java.util.*;
public class clothApp2
{
public static void main(String args[])
{
Clothes cloth[]=new Clothes[3];//declare an array to store 3 clothes information

String sz;
double price,disc;
Scanner input=new Scanner(System.in);
Scanner inputText=new Scanner(System.in);

for(int i=0;i<3;i++)
{ //input data
System.out.println("\nCloth "+(i+1)+":");
System.out.println("Enter Size:");
sz=inputText.nextLine();
System.out.println("Enter Price:");
price=input.nextDouble();
System.out.println("Enter Percent Discount(Eg:0.05 represent 5%):");
disc=input.nextDouble();
cloth[i]=new Clothes(sz,price,disc); //call normal constructor
//cloth[i].setClothes(sz,price,disc); //if used normal constructor,DO NOT call the setter
method
}

//Calculate the price after discount for each cloth


//double nPrice[]=new double[3];

//for(int i=0;i<3;i++)
//{
// nPrice[i]=cloth[i].calcNewPrice();
//}
//the process will be done during display the information

//Display the average value of normal price


double sum=0,avg;
for(int i=0;i<3;i++)
{ sum=cloth[i].getPrice();}
avg=sum/3;
System.out.println("Average value for normal price:"+avg);
//if method have return statement, we must call using output statement(System.out.println..)
//or using assignment statement eg, nPrice[i]=cloth[i].calcNewPrice()

//Display the detail of each cloth(using display() method including price after discount.
System.out.println("\nDetail for each cloth:");
for(int i=0;i<3;i++)
{ System.out.println("\nCloth:"+(i+1)+":");
cloth[i].display();
System.out.println("Price after discount:"+cloth[i].calcNewPrice());
}
}
}

You might also like