Question

In: Computer Science

I am trying to get this code to work but I am having difficulties, would like...

I am trying to get this code to work but I am having difficulties, would like to see if some one can solve it. I tried to start it but im not sure what im doing wrong. please explain if possible

package edu.hfcc;

/*
* Create Java application that will create Fruit class and Bread class
*
* Fruit class will have 3 data fields name and quantity which you can change.
* The third data field price should always be 2.0
* Bread class will have 3 data fields name, quantity and price. All three data fields can change.
*
* Create method that creates the string used write to console and return from execute()
*
* In GroceryApp
* Create method to calculate totalPrice for fruit
* Create method to calculate totalPrice for bread
* Create method to print to console use EXACLTY like example below
*
* Data to use
* Bread:     Name=French,    Quantity=2,   Price=3.0
* Fruit:   Name=Apple,    Quantity=5,   
*
* EXAMPLE TO Console
* French
        2 @ 3.0       6.0
Apple
      5 @ 2.0       10.0
---------------------
                   16.0
*/

public class Grocery {

   private static final char NEW_LINE = '\n';
   private static final String TAB = "\t";
  
   public String execute(){
      
       Fruit fruitOne = new Fruit();
       fruitOne.setName("Apple");
       fruitOne.setQuantity( 5);
      
       String fruits = constructOutputFruit(apple);
       System.out.println(fruits);

       return fruits;
   }
   private String constructOutputFruit(Fruit fruit1) {
       String apple = fruit1.getName() + NEW_LINE + fruit1.getQuantity() + TAB + fruit1.getPrice();
      
       return apple + NEW_LINE;
   }
}

here are my classes:

Fruit:

package edu.hfcc;

public class Fruit {

   private String name;
   private int quantity;
   private float price;
  
   public Fruit(){
       this.price = (float)2.0;
       }
  

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getQuantity() {
       return quantity;
   }

   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }

   public float getPrice() {
       return price;
   }
}

Bread:

package edu.hfcc;

public class Bread {

   private String name;
   private int quantity;
   private float price;
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getQuantity() {
       return quantity;
   }
   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }
   public float getPrice() {
       return price;
   }
   public void setPrice(float price) {
       this.price = price;
   }
  

}

Solutions

Expert Solution

class Fruit {
private String name;
private int quantity;
private float price;
  
public Fruit(){
this.price = (float)2.0;
}
  
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getPrice() {
return price;
}
}

class Bread {
private String name;
private int quantity;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
  
}


  

class GroceryApp
{
   public static void main (String[] args)
   {
  
Bread bread1 = new Bread();
bread1.setName("French");
bread1.setQuantity(2);
bread1.setPrice(3.0f);
  
System.out.println(bread1.getName() + "\n" + bread1.getQuantity() + "@"+ bread1.getPrice() +"\t"+totalPriceBread(bread1));

Fruit fruit1 = new Fruit();
fruit1.setName("Apple");
fruit1.setQuantity( 5);

System.out.println(fruit1.getName() + "\n" + fruit1.getQuantity() + "@"+ fruit1.getPrice() +"\t"+totalPriceFruit(fruit1));

System.out.println("------------------------------");
System.out.println("\t"+(totalPriceFruit(fruit1)+totalPriceBread(bread1)));
   }

  

public static float totalPriceFruit(Fruit f)
{
    return f.getQuantity() * f.getPrice();
}
public static float totalPriceBread(Bread b)
{
    return b.getQuantity() * b.getPrice();
}
}

      
Output:

French
[email protected]   6.0
Apple
[email protected]   10.0
------------------------------
        16.0

Do ask if any doubt. Please upvote.


Related Solutions

I am having difficulties with figuring out the formulas for this this question. Is there a...
I am having difficulties with figuring out the formulas for this this question. Is there a walk-through for this? You are the manager of a monopoly that sells a product to two groups of consumers in different parts of the country. Group 1’s elasticity of demand is -2, while group 2’s is -3. Your marginal cost of producing the product is $30. a. You are a monopolist. b. You compete against one other firm in a Cournot oligopoly. c. You...
I am having an issue with the code. The issue I am having removing the part...
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver. Source Code: Person .java File: public abstract class Person { private String name; /** * Constructor * @param name */ public Person(String name) { super(); this.name =...
I am in a statistics class. When I graduate, I would like to work in an...
I am in a statistics class. When I graduate, I would like to work in an area where I can help restructure businesses that are failing and make them successful. My professor has asked me where in my career that I think I will use the process of data collection. He has asked me to provide 2 examples. This is my first statistics class, so I don't really know of how to give him any examples. Can you please give...
How would I get this java code to work and with a main class that would...
How would I get this java code to work and with a main class that would demo the rat class? class rat { private String name; private String specialAbility; private int TotalHealth; private int shieldedHealth; private int cooldown; public rat() { } public rat(String n,String SA,int TH,int SH,int cd) { name=n; specialAbility=SA; TotalHealth=TH; shieldedHealth=SH; cooldown=cd; } public void setname(String n) { name=n; } public String getname() { return name; } public void setability(String SA) { specialAbility=SA; } public String getability()...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
I am having some difficulties finding the IRR and NPV, and the discounted payback period of...
I am having some difficulties finding the IRR and NPV, and the discounted payback period of a Capital project for Intel that will last 12 yrs: Project Cost; 10% of PPE : 4,897,600 Annual Depreciation: 387,726.67 /yr over 12 yrs Annual EBIT: 881,568 WACC: 6.55% Free Cash Flows (Year 1-12): 825,038.54
Assembly Question: I am trying to annotate this code and am struggling to understand what it...
Assembly Question: I am trying to annotate this code and am struggling to understand what it is doing. Can someone please add comments? .data .star: .string "*" .line: .string "\n" .input: .string "%d" .count: .space 8 .text .global main printstars: push %rsi push %rdi _printstars: push %rdi mov $.star, %rdi xor %rax, %rax call printf pop %rdi dec %rdi cmp $0, %rdi jg _printstars mov $.line, %rdi xor %rax, %rax call printf pop %rdi pop %rsi ret printstarpyramid: mov %rdi,...
I am having problems with : If 6 is selected, then you will get an employee...
I am having problems with : If 6 is selected, then you will get an employee pay amount from the user. This will be a double. Only allow them to enter one pay amount and then display the main menu again. AND: if 2 is selected you will promt the user for a grade (double) from 0-100. Only allow them to enter one grade and then display the main menu again The project: Create a project called P03. Inside of...
Hello, I am very new to 64-bit ARM assembly and would like the code to this...
Hello, I am very new to 64-bit ARM assembly and would like the code to this following question: The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT