Question

Task 01: (a)Write a class Fruit with: private instance variables name, and pricePerKilogram Appropriate constructor Appropriate...

Task 01:

(a)Write a class Fruit with:

  • private instance variables name, and pricePerKilogram
  • Appropriate constructor
  • Appropriate accessor methods
  • Appropriate mutator methods
  • toString method
  • equals method

(b) Write a FruitDriver class that:

  • Initializes an array of Fruit objects, fruitArray, with 10 objects with names: banana, apple, mango, orange,pineapple, pear, grapes, tangerine, watermelon, sweetmelon and appropriate prices per kilogram.
  • Uses an appropriate loop to display all objects with pricePerKilogram > 5.00 Saudi Riyals, if any.
  • Calls a linearSearch method:   

                    public static boolean findFruit(Fruit targetFruit, Fruit[] fruitArray)

                the method ruturnstrue if targetFruit is in the fruitArray; otherwise it returns false.

  • Displays the result of the search using an appropriate message

Task 02:

Write a Java application that initializes an array of Strings with the following strings:

          "Hot Peppers"      "Onions"    "Milk"   "Eggs"   "Rice"

The program must then use a loop to get the first character of each string in the array and make a new word with those letters. The program finally prints the new word.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code for task 1

=====================================================================================

class Fruit
{
   private String name;
   private float pricePerKilogram;
  
  
  
   public Fruit(String name, float pricePerKilogram) {
       this.name = name;
       this.pricePerKilogram = pricePerKilogram;
   }
   public String getName() {
       return name;
   }
   public float getPricePerKilogram() {
       return pricePerKilogram;
   }
   public void setName(String name) {
       this.name = name;
   }
   public void setPricePerKilogram(float pricePerKilogram) {
       this.pricePerKilogram = pricePerKilogram;
   }
  
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Fruit other = (Fruit) obj;
       if (name == null) {
           if (other.name != null)
               return false;
       } else if (!name.equals(other.name))
           return false;
       if (Float.floatToIntBits(pricePerKilogram) != Float
               .floatToIntBits(other.pricePerKilogram))
           return false;
       return true;
   }
   @Override
   public String toString() {
       return "Fruit [name=" + name + ", pricePerKilogram=" + pricePerKilogram
               + "]";
   }
     
  
  
  
}
public class FruitDriver {
  
   public static boolean findFruit(Fruit targetFruit, Fruit[] fruitArray)
   {
       boolean b=false;
       for(int i=0;i<fruitArray.length;i++)
       {
           if(fruitArray[i].equals(targetFruit))
           {
               b=true;
               break;
           }
       }
      
      
       return b;
   }
     

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Fruit f[]=new Fruit[10];
       String s[]={"banana","apple","mango","ornage","pineapple","pear","grapes","tangerine","watermelon","sweetmelon"};
       float price[]={4.5f,100f,200f,30f,78f,2.0f,89f,34f,45f,34f};
       for(int i=0;i<10;i++)
       {
           f[i]=new Fruit(s[i],price[i]);
       }
      
       for(int i=0;i<10;i++)
       {
           if(f[i].getPricePerKilogram()>5.00f)
           {
               System.out.println(f[i]);
           }
       }
      
       boolean b= findFruit(f[2],f);
       if(b==true)
       System.out.println(f[2]+"is present in Fruit array");
       else
           System.out.println(f[2]+"is not present in Fruit array");
      
       Fruit temp=new Fruit("cheery",342.2f);
       b= findFruit(temp,f);
       if(b==true)
       System.out.println(temp+"is present in Fruit array");
       else
           System.out.println(temp+"is not present in Fruit array");
      
   }

}

=====================================================================================

Output

Add a comment
Know the answer?
Add Answer to:
Task 01: (a)Write a class Fruit with: private instance variables name, and pricePerKilogram Appropriate constructor Appropriate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT