Question

In: Computer Science

JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the...

JAVA CODE

Define a method called changeGroupPrice that could be added to the definition of the class Purchase. This method has one parameter that is of type double, and is named salePercent. This number represents the percent reduction in the groupPrice amount.   The method uses this number to change the groupPrice. The code of the method should check to make sure the range of salePercent is between 0 and 50%. If it is, the groupPrice should be adjusted accordingly. If the salePercent is out of range of the accepted values an error message should be printed and the program terminated.

public class PurchaseDemo

{ public static void main(String[] args)

{

Purchase oneSale = new Purchase();

oneSale.readInput();

oneSale.writeOutput();

System.out.println("Cost each $" + oneSale.getUnitCost());

System.out.println("Total cost $" + oneSale.getTotalCost());

}

}

import java.util.Scanner;
/**   
Class for the purchase of one kind of item, such as 3 oranges.
Prices are set supermarket style, such as 5 for $1.25.
*/
public class Purchase   
{
private String name;
private int groupCount; //Part of a price, like the 2 in //2 for $1.99.
private double groupPrice; //Part of a price, like the $1.99
// in 2 for $1.99.
private int numberBought; //Number of items bought.
public void setName(String newName)
{
name = newName;
}
/**   
Sets price to count pieces for $costForCount.   
For example, 2 for $1.99.   
*/
public void setPrice(int count, double costForCount)
{
if ((count <= 0) || (costForCount <= 0))
{
System.out.println("Error: Bad parameter in " +
"setPrice.");
System.exit(0);
}
else
{
groupCount = count;
groupPrice = costForCount;
}
}
public void setNumberBought(int number)
{
if (number <= 0)
{
System.out.println("Error: Bad parameter in " +
"setNumberBought.");
System.exit(0);
}
else
numberBought = number;
}
/**
Reads from keyboard the price and number of a purchase.
*/   
public void readInput()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter name of item you are purchasing:");
name = keyboard.nextLine();
System.out.println("Enter price of item as two numbers.");
System.out.println("For example, 3 for $2.99 is entered as");
System.out.println("3 2.99");
System.out.println("Enter price of item as two numbers, " +
"now:");
groupCount = keyboard.nextInt();
groupPrice = keyboard.nextDouble();
while ((groupCount <= 0) || (groupPrice <= 0))
{ //Try again:
System.out.println("Both numbers must " +
"be positive. Try again.");
System.out.println("Enter price of " +
"item as two numbers.");
System.out.println("For example, 3 for " +
"$2.99 is entered as");
System.out.println("3 2.99");
System.out.println(
"Enter price of item as two numbers, now:");
groupCount = keyboard.nextInt();
groupPrice = keyboard.nextDouble();
}
System.out.println("Enter number of items purchased:");
numberBought = keyboard.nextInt();
while (numberBought <= 0)
{ //Try again:
System.out.println("Number must be positive. " +
"Try again.");
System.out.println("Enter number of items purchased:");
numberBought = keyboard.nextInt();
}
}
/**   
Displays price and number being purchased.
*/
public void writeOutput()   
{
System.out.println(numberBought + " " + name);
System.out.println("at " + groupCount +
" for $" + groupPrice);
}
public String getName()
{
return name;
}
public double getTotalCost()
{
return (groupPrice / groupCount) * numberBought;
}
public double getUnitCost()
{
return groupPrice / groupCount;
}
public int getNumberBought()
{
return numberBought;
}
}

Solutions

Expert Solution

Purchase.java

import java.util.Scanner;

/**
* Class for the purchase of one kind of item, such as 3 oranges. Prices are set
* supermarket style, such as 5 for $1.25.
*/
public class Purchase {
   private String name;
   private int groupCount; // Part of a price, like the 2 in //2 for $1.99.
   private double groupPrice; // Part of a price, like the $1.99
// in 2 for $1.99.
   private int numberBought; // Number of items bought.

   public void setName(String newName) {
       name = newName;
   }

   /**
   * Sets price to count pieces for $costForCount. For example, 2 for $1.99.
   */
   public void setPrice(int count, double costForCount) {
       if ((count <= 0) || (costForCount <= 0)) {
           System.out.println("Error: Bad parameter in " + "setPrice.");
           System.exit(0);
       } else {
           groupCount = count;
           groupPrice = costForCount;
       }
   }

   public void setNumberBought(int number) {
       if (number <= 0) {
           System.out.println("Error: Bad parameter in " + "setNumberBought.");
           System.exit(0);
       } else
           numberBought = number;
   }

   /**
   * Reads from keyboard the price and number of a purchase.
   */
   public void readInput() {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Enter name of item you are purchasing:");
       name = keyboard.nextLine();
       System.out.println("Enter price of item as two numbers.");
       System.out.println("For example, 3 for $2.99 is entered as");
       System.out.println("3 2.99");
       System.out.println("Enter price of item as two numbers, " + "now:");
       groupCount = keyboard.nextInt();
       groupPrice = keyboard.nextDouble();
       while ((groupCount <= 0) || (groupPrice <= 0)) { // Try again:
           System.out.println("Both numbers must " + "be positive. Try again.");
           System.out.println("Enter price of " + "item as two numbers.");
           System.out.println("For example, 3 for " + "$2.99 is entered as");
           System.out.println("3 2.99");
           System.out.println("Enter price of item as two numbers, now:");
           groupCount = keyboard.nextInt();
           groupPrice = keyboard.nextDouble();
       }
       System.out.println("Enter number of items purchased:");
       numberBought = keyboard.nextInt();
       while (numberBought <= 0) { // Try again:
           System.out.println("Number must be positive. " + "Try again.");
           System.out.println("Enter number of items purchased:");
           numberBought = keyboard.nextInt();
       }
   }
  
   public boolean changeGroupPrice(double salePercent) {
      
       if(salePercent>=0 && salePercent<=50) {
           this.groupPrice = this.groupPrice*(1-(salePercent/100));
           return true;
       }
      
       else {
           System.out.println("SalePercent is not in the range [0,50]");
           return false;
       }
      
      
   }

   /**
   * Displays price and number being purchased.
   */
   public void writeOutput() {
       System.out.println(numberBought + " " + name);
       System.out.println("at " + groupCount + " for $" + groupPrice);
   }

   public String getName() {
       return name;
   }

   public double getTotalCost() {
       return (groupPrice / groupCount) * numberBought;
   }

   public double getUnitCost() {
       return groupPrice / groupCount;
   }

   public int getNumberBought() {
       return numberBought;
   }
}

PurchaseDemo.java

import java.util.Scanner;

public class PurchaseDemo

{ public static void main(String[] args)

{

Purchase oneSale = new Purchase();

oneSale.readInput();

System.out.println("Enter the salePercent (It must be between 0 and 50) : ");
Scanner scan = new Scanner(System.in);
double sp = scan.nextDouble();

boolean flag = oneSale.changeGroupPrice(sp);


if(flag) {

oneSale.writeOutput();

System.out.println("Cost each $" + oneSale.getUnitCost());

System.out.println("Total cost $" + oneSale.getTotalCost());
}

}

}

Output

TestRun-1

Enter name of item you are purchasing:
apple
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
2 3.00
Enter number of items purchased:
2
Enter the salePercent (It must be between 0 and 50) :
50
2 apple
at 2 for $1.5
Cost each $0.75
Total cost $1.5

TestRun-2

Enter name of item you are purchasing:
apple
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
2 3.00
Enter number of items purchased:
2
Enter the salePercent (It must be between 0 and 50) :
51
SalePercent is not in the range [0,50]

Sreenshot

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful


Related Solutions

JAVA CODE Give the definition of a static method called showArray that has an array of...
JAVA CODE Give the definition of a static method called showArray that has an array of base type char as a single parameter and that writes to the screen each character in the array on a separate line. It then writes the same array characters in reverse order. This method returns a character array that holds these two lines of characters in the order that you printed them.
JAVA CODE Give the definition of a static method called showArray that has an array of...
JAVA CODE Give the definition of a static method called showArray that has an array of base type char as a single parameter and that writes to the screen each character in the array on a separate line. It then writes the same array characters in reverse order. This method returns a character array that holds these two lines of characters in the order that you printed them.
Java Define a class called BlogEntry that could be used to store an entry for a...
Java Define a class called BlogEntry that could be used to store an entry for a Web log. The class should have instance variables to store : - the poster’s username, - text of the entry, - and the date of the entry using the Date class Date class is: class Date { private int day, year; private String mon; public Date() { mon=" "; day=0; year=0; } public String toString() { return (mon+"/"+day+"/"+year); } public set_date(String m, int d,...
JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance...
JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance variables of the class for equality. One is in the Purchase class and the other is a static method of the main. Give sample calls for each. public class PurchaseDemo { public static void main(String[] args) { Purchase oneSale = new Purchase(); oneSale.readInput(); oneSale.writeOutput(); System.out.println("Cost each $" + oneSale.getUnitCost()); System.out.println("Total cost $" + oneSale.getTotalCost()); } } import java.util.Scanner; /**    Class for the purchase...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode searchBST(TreeNode root, int val) {    }...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: TreeNode* sortedArrayToBST(vector<int>& nums) {    } }; Given an array where elements are sorted in...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<int> inorderTraversal(TreeNode* root) {    } }; Given the root of a binary tree, return...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<int> preorderTraversal(TreeNode* root) {    } }; Given the root of a binary tree, return...
JAVA CODE // Write a method called example that will do several things. // It has...
JAVA CODE // Write a method called example that will do several things. // It has two integer array parameters of unknown varying lengths. // It returns an integer array that contains each of the first array values // divided by two and each of the second array values added to the number 100. // It prints each value of the first array and then prints that value // divided by two on a separate line. It then prints each...
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT