Question

In: Computer Science

Java Program General Scenario: You are creating an interactive application that gets input from the keyboard...

Java Program General Scenario: You are creating an interactive application that gets input from the keyboard and keeps track of a grocery store that a businessman runs. In order to develop this application, you will be using 2 interfaces, 1 abstract class, and 1 concrete class. You should use the interfaces and the abstract class to create the concrete class. Your application should have overridden toString() at appropriate places so that you could display the object state, i.e., the string representation of the objects that you create by simply using the following command: System.out.println(object). Follow the following instructions: Create 2 interfaces: Onion and Coke. Interface Onion contains only 1 abstract method: onionPrice() which does not return any data. It takes 1 double argument: onionPrice that represents the current price (per kilo) of onion. Interface Coke contains only 1 abstract method: cokePrice() which does not return any data. It takes 1 double argument: cokePrice that represents the current price (per bottle) of coke. Create an abstract class named Businsess. This class contains 1 instance variable, 1 constructor, and 1 abstract method. Instance variable: numEmp (that will take care of the number of employees in the business. An 1-argument constructor that will be called from its subclass to load 1 instance variable of this level during the creation of an object in its subclass. 1 abstract method: numEmployee(). This method does not take any arguments. When implemented in its subclass, it is supposed to return an integer that represents the number of employees in the business. In this abstract class Business, write necessary code to get the object state (i.e., the string representation) that will eventually be called from its concrete subclass. Create a concrete class named GroceryBusiness that makes use of both the interfaces: Onion and Coke that you created earlier. This concrete class also makes use of the abstract class Business that you also created earlier. a. This concrete class GroceryBusiness has 1 instance variable of its own, 1 constructor, and 1 concrete method of its own. Instance variable: groceryIncome (Use appropriate data type) that represents the current yearly income of the grocery business. Constructor: 1 constructor that constructs a GroceryBusiness object with all the its instance variables (numEmp and groceryIncome) supplied during the GroceryBusiness object creation. 1 concrete method of its own: yearEstablished() that does not return any data. It takes 1 integer argument (year) that represents the year the grocery business was established. This method displays the year the grocery business was established from within itself. Write all necessary code to make this class a concrete class. Write necessary code to get the complete string representation (i.e., the object state) of objects made from this class. [35 marks] Create a driver class named XXXMidTest (replace XXX by three letters of your name). This driver class would prompt the user to enter current number of employees in your grocery business. It would then ask how much you earn from the grocery business. It then uses these 2 user input to create a GroceryBusiness object. It would then display the string representation of the object you just created. Then it would ask for the year the grocery business was established.. It would display the year you just entered. It would then ask for the current price of onion per kilo. And finally it would ask for the current price of coke per bottle. It would then display the prices you just entered.

Solutions

Expert Solution

1)Onion interface:

//interface
public interface Onion {

   //only 1 abstract method:
   void onionPrice(double onionPrice);
}

2)Coke interface:

//interface
public interface Coke {

   //only 1 abstract method
   void cokePrice(double cokePrice);
}

3)Business class:

//an abstract class named Business.
public abstract class Business {

   //Instance variable
   int numEmp;
  
   //An 1-argument constructor
   public Business(int numEmp) {
       this.numEmp = numEmp;
   }
  
   //1 abstract method: numEmployee(). This method does not take any arguments.
   public abstract int numEmployee();

   //toString() method
   @Override
   public String toString() {
       return "Business [Number of employees in the business:" + numEmp + "]";
   }
}

4)GroceryBusiness class:


/*This class makes use of both the interfaces: Onion and Coke
also makes use of the abstract class Business */
public class GroceryBusiness extends Business implements Onion, Coke {

   //instance variable
   private double groceryIncome;
  
  
   //An 2-argument constructor which calls super(Business) class constructor
   public GroceryBusiness(int numEmp, double groceryIncome) {
       //calling super(Business) class constructor
       super(numEmp);
       this.groceryIncome = groceryIncome;
   }

   //Cock interface abstract method implementation
   @Override
   public void cokePrice(double cokePrice) {
       System.out.println("Current price (per bottle) of coke:"+cokePrice+".");
   }

   //Onion interface abstract method implementation
   @Override
   public void onionPrice(double onionPrice) {
       System.out.println("Current price (per kilo) of onion:"+onionPrice+".");
   }

   //Business abstract class abstract method implementation
   @Override
   public int numEmployee() {
       return this.numEmp;
   }

  
   //1 concrete method
   void yearEstablished(int year) {
       System.out.println("Grocery business was established in the year "+year+".");
   }

   //getter and setter methods for groceryIncome
   public double getGroceryIncome() {
       return groceryIncome;
   }

   public void setGroceryIncome(double groceryIncome) {
       this.groceryIncome = groceryIncome;
   }

   @Override
   public String toString() {
       return "GroceryBusiness [Grocery Income:" + groceryIncome + ", Number of employees in the business:" + numEmployee() +"]";
   }
  
  
}

5)JohnMidTest class:

import java.util.Scanner;

//Driver class
public class JohnMidTest {
  
   //main method
   public static void main(String[] args) {
       //Scanner object to read input from keyboard
       Scanner scan = new Scanner(System.in);
      
       //asking or prompting for user input
       System.out.print("Enter current number of employees in your grocery business:");
       int numEmp = scan.nextInt();
      
       System.out.print("How much you earn from the grocery business:");
       double groceryIncome = scan.nextDouble();
      
       //uses these 2 user input to create a GroceryBusiness object
       GroceryBusiness groceryBusiness = new GroceryBusiness(numEmp, groceryIncome);
      
       //display the string representation of the object you just created.
       System.out.println(groceryBusiness);
      
       System.out.print("Year the grocery business was established:");
       int year = scan.nextInt();
      
       //display the year you just entered.
       groceryBusiness.yearEstablished(year);
      
       System.out.print("Current price of onion per kilo:");
       double onionPrice = scan.nextDouble();
       System.out.print("Current price of coke per bottle:");
       double cokePrice = scan.nextDouble();
      
       // display the prices you just entered.
       groceryBusiness.onionPrice(onionPrice);
       groceryBusiness.cokePrice(cokePrice);
   }
}

Output:


Related Solutions

Write an interactive Java class which accepts an input argument when the application is executed from...
Write an interactive Java class which accepts an input argument when the application is executed from the command-line. Accept input from the user and compare the value entered to the command-line argument value. If the strings do not equal, display "INVALID VALUE! TRY AGAIN!", otherwise display "PERMISSION GRANTED!" and exit the program.
Create in java an interactive GUI application program that will prompt the user to use one...
Create in java an interactive GUI application program that will prompt the user to use one of three calculators, label project name MEDCALC. You can use any color you want for your background other than grey. Be sure to label main java box with “MEDCALC” and include text of “For use only by students” On all three calculators create an alert and signature area for each user. The alert should be something like “All calculations must be confirmed by user...
Write an interactive C program that asks a user to enter a sentence from the keyboard,...
Write an interactive C program that asks a user to enter a sentence from the keyboard, and prints the sentence with all the words spelled backwards. Note: you may assume that each sentence will have at most 50 characters and each word is separated by a space. Sample code execution: bold information entered by user enter a sentence: hello ece 175 class olleh ece 571 ssalc continue (q to quit)? y enter a sentence: May the force be with you...
Write a program that first gets a list of integers from input. The input begins with...
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain fewer than 20 integers. That list is followed by two more integers representing lower and upper bounds of a range. Your program should output all integers from the list that are within that range (inclusive of the bounds). For coding simplicity, follow each output integer by a space,...
Write a program that prompts the user to input their first name from the keyboard and...
Write a program that prompts the user to input their first name from the keyboard and stores them in the variable "firstName". It does the same for last name and stores it in the variable "lastName". It then uses strcat to merge the two names, separates them by a space and stores the full name into a string variable called "fullName". In the end, the program must print out the string stored within fullName. ANSWER IN C LANGUAGE ! You...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
Using Java create an interactive (the user should be able to input the values) Java Application to process your utility bill for at-least 6 months.
Java ProgrammingAssignment 7.1 (25 points)Using Java create an interactive (the user should be able to input the values) Java Application to process your utility bill for at-least 6 months. Use Class-Object methodology and use single dimensional arrays in your application. Write detailed comments in your program. You should have a two classes (for example, MontlyBill.java & MontlyBillTest.java). You can also be creative and create your “own problem scenario” and come up with your “solution”.Assignment 7.2 (25 points)Use the same program...
in java Write an application that gets two numbers from the user and prints the sum,...
in java Write an application that gets two numbers from the user and prints the sum, product, difference and quotient of the two numbers in a GUI.
Java ProgrammingAssignment 4.1Create an interactive (the user should be able to input thevalues)...
Java ProgrammingAssignment 4.1Create an interactive (the user should be able to input the values) Java Application to process your utility bill for at-least 6 months. Use Class-Object methodology and use single dimensional arraysin your application. Write detailed comments in your program. You should have a two classes (for example, MontlyBill.java & MontlyBillTest.java)..Hint: Use the program (GradeBook) in Fig 7.14 & 7.15(GradeBookTest) as a building template.Assignment 4.2Use the same program that you have created in 4.1, and use “two dimensional array”....
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT