In Java Create a class named \"Price\", this class contains three private fields
ID: 3816248 • Letter: I
Question
In Java
Create a class named "Price", this class contains
three private fields: price, quantity, couponvalue
Methods to get and set each of the fields
one constructor with three parameters, these three parameters can initialize price, quantity and coupon value as specified (for instance, 20, 10, 5)
computeprice() method with three parameters, they represent the price of a meal, the quantity ordered and a coupon value. Multiple the price and quantity, reduce the result by the coupon value, and then add 20% tip and return the total price.
The class should contain a main() method which tests constructor method, get and set methods, and computeprice() methoduse system.out.println() to show different results
Explanation / Answer
CODE:
import java.util.Scanner;
//import org.junit.Assert; Add scope to your make file, otherwise this won't run
public class Price
{
private float price;
private int quantity;
private float couponvalue;
public Price(float price, int quantity, float couponvalue) // Constructor
{
this.price = price;
this.quantity = quantity;
this.couponvalue = couponvalue;
}
//Set-Get methods
public void setprice(float price)
{
this.price = price;
}
public float getprice()
{
return this.price;
}
public void setquantity(int quantity)
{
this.quantity = quantity;
}
public int getquantity()
{
return this.quantity;
}
public void setcouponvalue(float couponvalue)
{
this.couponvalue = couponvalue;
}
public float getcouponvalue()
{
return this.couponvalue;
}
//compute price method
public float computeprice(float price, int quantity, float couponvalue)
{
float totalprice = (price * quantity) - couponvalue;
totalprice = totalprice + (totalprice / 5); // adding 20% = 20/100 = 1/5
return totalprice;
}
//Driver code
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
float price;
int quantity;
float couponvalue;
float totalprice;
//Taking input from user
System.out.print("Enter price of meal: ");
price = scanner.nextFloat();
System.out.print("Enter quantity of meal: ");
quantity = scanner.nextInt();
System.out.print("Enter coupon value: ");
couponvalue = scanner.nextFloat();
// creating an object
Price meal = new Price(price, quantity, couponvalue);
//Price meal1 = new Price(); throws error
/*System.out.println("Test constructor Price: "+ meal.getprice() + " Quantity: " + meal.getquantity() + " Coupon value: " + meal.getcouponvalue());
meal.setprice(price);
meal.setquantity(quantity);
meal.setcouponvalue(couponvalue);
System.out.println("Test get-set Price: " + Assert.assertEquals(meal.getprice(),price,0.0f) ? "Pass" : "Fail" + " Quantity: " + Assert.assertEquals(meal.getquantity(),quantity) ? "Pass" : "Fail" + " Coupon value: " + Assert.assertEquals(meal.getcouponvalue(),couponvalue,0.0f) ? "Pass" : "Fail");*/
//compute price
totalprice = meal.computeprice(meal.price, meal.quantity, meal.couponvalue);
System.out.println("Total price is "+ totalprice);
}
}
NOTE: Testing the get-set methods aren't really helpful. However if you still want to test them, uncomment the commented part and make sure you have juint in your eclipse or IDE.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.