In: Computer Science
CODE IN JAVA:
Write a ProductTester class that creates one Product object named "Juicer" with a price of $50.99, prints the name and price of the product, reduces its price by 4.0, and then prints its price again.
public class Product {
private String name;
private double price;
public Product(String productName, double productPrice) {
name = productName;
price = productPrice;
}
public String getName() {
return name;
}
public double getPrice()
{
return price;
}
public void reducePrice(double amount) {
price = price - amount;
}
}
}
NOTE: THE PROGRAM IS DONE IN TWO WAYS
1. HERE THE NAME IS TAKEN FROM CONSOLE INPUT.
2. NAME IS DIRECTLY PUT AT THE TIME OF OBJECT INITIALIZATION THROUGH CONSTRUCTOR.
1. PROGRAM WHERE NAME IS TAKEN FROM CONSOLE INPUT.
/* import scanner class with util package */
import java.util.Scanner;
/* here as per the question public class Product will be
changed to class Product because here file name is
ProductTester.java, so class ProductTester
will be public, not the class Product */
class Product
{
private String name;
private double price;
public Product(String productName, double productPrice)
{
name = productName;
price = productPrice;
}
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
public void reducePrice(double amount)
{
price = price - amount;
}
}
/* ProductTester class is defined */
public class ProductTester
{
public static void main(String[] args)
{
String name;
/* sc is the object of scanner
class */
/* sc is used to take input from
console */
Scanner sc=new
Scanner(System.in);
/* name is taken from console input
*/
System.out.print("Enter the name :
");
name=sc.nextLine();
/* Juicer is object of class
Product */
Product Juicer=new
Product(name,50.99);
/* print name */
System.out.println("Name is : " +
Juicer.getName());
/* print price */
System.out.println("Price is : " +
Juicer.getPrice());
Juicer.reducePrice(4.0);
/* print new price */
System.out.println("New Price is :
" + Juicer.getPrice());
}
}
SCREEN SHOT
OUTPUT
2.PROGRAM WHERE NAME IS DIRECTLY PUT AT THE TIME OF OBJECT INITIALIZATION THROUGH CONSTRUCTOR.
/* import scanner class with util package */
import java.util.Scanner;
/* here as per the question public class Product will be
changed to class Product because here file name is
ProductTester.java, so class ProductTester
will be public, not the class Product */
class Product
{
private String name;
private double price;
public Product(String productName, double productPrice)
{
name = productName;
price = productPrice;
}
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
public void reducePrice(double amount)
{
price = price - amount;
}
}
/* ProductTester class is defined */
public class ProductTester
{
public static void main(String[] args)
{
String name;
/* sc is the object of scanner
class */
/* sc is used to take input from
console */
Scanner sc=new
Scanner(System.in);
/* Juicer is object of class
Product */
Product Juicer=new
Product("Antony",50.99);
/* print name */
System.out.println("Name is : " +
Juicer.getName());
/* print price */
System.out.println("Price is : " +
Juicer.getPrice());
Juicer.reducePrice(4.0);
/* print new price */
System.out.println("New Price is :
" + Juicer.getPrice());
}
}
SCREEN SHOT
OUTPUT