Question

In: Computer Science

CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a...

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;

}

}

}

Solutions

Expert Solution

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


Related Solutions

3.1 Write code that creates an ArrayList object named list and fills list with these numbers...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers (using one or a pair of for or while loops): 0 1 2 3 4 0 1 2 3 4 3.2 Consider the ArrayList object named list containing these Integers: list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 } What are the contents of list after this loop completes? for (int i = 1; i < 10; ++i) {...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
Java - Write a test program that creates an Account object with an account number of...
Java - Write a test program that creates an Account object with an account number of AC1111, a balance of $25,000, and an annual interest rate of 3.5. Use the withdraw method to withdraw $3,500, use the deposit method to deposit $3,500, and print the balance, the monthly interest, and the date when this account was created.
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
rite the code for a class named Funnel. A funnel is a cone-shaped object that is...
rite the code for a class named Funnel. A funnel is a cone-shaped object that is used to pour liquids into small openings. For the purpose of this question, you may assume that a Funnel is an inverted cone (see image at left) and has a flap (a lid) at the bottom that can be opened and closed. A Funnel has the following PUBLIC features: a) A Funnel created without any data has a radius of 12, a height of...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                            &n
Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                                                              - meanings: String + DictionaryWord (String word, String meanings) + getWord(): String + setWord (String word): void + getMeanings(): String + setMeanings(String meanings): void Write a program with the following requirements: Creates 8 DictionaryWord objects with: Word and meanings as the table: word meanings bank robber Steals money from a bank burglar Breaks into a home to steal things forger Makes an illegal copy of something...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this: 2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT