Task 01:
(a)Write a class Fruit with:
(b) Write a FruitDriver class that:
public static boolean findFruit(Fruit targetFruit, Fruit[] fruitArray)
the method ruturnstrue if targetFruit is in the fruitArray; otherwise it returns false.
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.
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
Task 01: (a)Write a class Fruit with: private instance variables name, and pricePerKilogram Appropriate constructor Appropriate...
Write a class named PhoneBookEntry that has fields for a person's name and phone number.The class should have a constructor and appropriate accessor and mutator methods. Thenwrite a program that creates at least five PhoneBookEntry objects and stores them in anArrayLitt. Use a loop to display the contents of each object in the ArrayList.
Design a class named Person with fields for holding a person's name, address, and telephone number (all as Strings). Write a constructor that initializes all of these values, and mutator and accessor methods for every field. Next, design a class named Customer, which inherits from the Person class. The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes...
Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...
Phone Book ArrayList Write a class named PhoneBookEntry that has fields for a person’s name and phone number. The class should have a constructor and appropriate accessor and mutator methods. Then write a program that creates at least five PhoneBookEntry objects and stores them in an ArrayList. Use a loop to display the contents of each object in the ArrayList Copyright | Addison-Wesley | Starting Out with Java | [email protected] | Printed from Chegg iOS App
Create a Ruby class Book1. Each book is characterized by the instance variables authnr rf t class . For this class create the initializer and the method show1 that displays the instance variables of the Bookl. class Create the class Book2 that inherits properties from the class Bookl and ad variables publisher and price. Write the following Book2 methods e read2 that prompts the user and reads from the keyboard all data for Book e show2 that displays all instance...
In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....
Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed to the constructor Methods: Accessors int getID( ) String getName( ) Class Roster: This class will implement the functionality of all roster for school. Instance Variables a final int MAX_NUM representing the maximum number of students allowed on the roster an ArrayList storing students Constructors a default constructor should initialize the list to empty strings a single parameter constructor that takes an ArrayList<Student> Both...
Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named CashPayment that is...
In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...
Java Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have - four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set...