Question

Hello,

In need of help with this Java pa homework. Thank you!

2. Create a normal (POJo, JavaBean) class to represent, i. e. model, varieties of green beans (also known as string beans or

4. In method main of class Main, (a) create an array of your green bean class type that can hold 10 elements of green bean cl

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

Main.javaVarBeans,java 1 package new11; 2 3 import java.text.DecimalFormat; 4 5 public class Main 7 //method print out the in

Main.java:

package new11;

import java.text.DecimalFormat;

public class Main {

//method print out the information about each beans
public static void infoGreenBean(VarBeans[] green) {

for (int i = 0; i < green.length; i++) {

try{
  
//condition for null or zero
if (green[i].getVariety() != null && green[i].getLength() != 0 && green[i].getSeedNum() != 0) {
  
//toString method for return the information about each bean
System.out.println("Beans "+ (i+1) + " has : "+green[i].toString());
}
  
}catch (Exception e) {
System.out.println("Beans "+ (i+1) + " has : "+"No information availabel for this type of beans");
}
}
}
//method return the average length of beans
public static double averageLength(VarBeans[] green) {

double avgLength = 0;
double totalLenght = 0;
for (int i = 0; i < green.length; i++) {
try{
if(green[i].getLength()!=0){
totalLenght = totalLenght + green[i].getLength();
}
}catch (Exception e) {
  
}
}
avgLength = totalLenght/green.length;
return avgLength;
}

//method return the avg number of seeds in the beans
public static double averageNumSeeds(VarBeans[] green) {

double avgNumSeeds = 0;
double totalSeeds = 0;
//for loop for iterate the array
for (int i = 0; i < green.length; i++) {
try{
if(green[i].getSeedNum()!=0){
totalSeeds = totalSeeds + green[i].getSeedNum();
}
}catch (Exception e) {
  
}
}
avgNumSeeds = totalSeeds/green.length;
return avgNumSeeds;
}

public static void main(String[] args) {

//This is for limit the decimal places
DecimalFormat numberFormat = new DecimalFormat("#.00");
//Creating array of VarBeans class
VarBeans[] greenBeans = new VarBeans[10];
//Add beans in the object
greenBeans[0] = new VarBeans("string beans", 8.0, 23);
greenBeans[1] = new VarBeans("filet beans", 4.5, 12);
greenBeans[2] = new VarBeans("long beans", 13.7, 30);
greenBeans[3] = new VarBeans("purple string beans", 5.5, 22);
greenBeans[4] = new VarBeans("flat beans", 2.5, 14);
greenBeans[5] = new VarBeans("wax beans", 3.5, 15);
greenBeans[6] = new VarBeans("bush beans", 4.5, 18);
  
//calling three methods
infoGreenBean(greenBeans);
double avgLength = averageLength(greenBeans);
double avgSeeds = averageNumSeeds(greenBeans);
//printing non void method's returned value
System.out.println("Average length of Beans is: "+numberFormat.format(avgLength));
System.out.println("Average number of seeds in Bean class: "+ numberFormat.format(avgSeeds));
}

}

VarBeans.java:

package new11;

public class VarBeans {

   //private fields
   private String variety;
   private double length;
   private double seedNum;

   //default constructor
   public VarBeans() {
   variety = "";
   length = 0;
   seedNum = 0;
   }

   /**
   *
   * @param variety
   * @param length
   * @param seedNum
   */
   //Parameterized constructor
   public VarBeans(String variety, double length, double seedNum) {
   super();
   this.variety = variety;
   this.length = length;
   this.seedNum = seedNum;
   }

   //getter and setter
   public String getVariety() {
   return variety;
   }

   public void setVariety(String variety) {
   if (!variety.isEmpty()) {
   this.variety = variety;
   }
   }

   public double getLength() {
   return length;
   }

   public void setLength(double length) {
   if (length > 0) {
   this.length = length;
   }
   }

   public double getSeedNum() {
   return seedNum;
   }

   public void setSeedNum(double seedNum) {
   if (seedNum > 0) {
   this.seedNum = seedNum;
   }
   }

   //toString method
   @Override
   public String toString() {
   return "variety=" + variety + ", length=" + length + ", seedNum=" + seedNum;
   }
  

   }

Add a comment
Know the answer?
Add Answer to:
Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal...
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
  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Please do this in C++. Thank you in advance. Note that you have to carefully create...

    Please do this in C++. Thank you in advance. Note that you have to carefully create your class using the names and capitalization shown below. Put comments into your program, make sure you indent your program properly, and use good naming conventions. Create a class called entry. It should have two private data members of type std::string. One represents a name and the other represents an email address. Make sure you give them ,meaningful names. Create public getters and setters...

  • I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...

    I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • Java Object Array With 2 Elements In 1 Object

    1. Create a UML diagram to help design the class described in Q2 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Person class object; should they be private or public? Determine what class methods are required; should they be private or public?2. Write Java code for a Person class. A Person has a name of type String and an age of type integer.Supply two constructors: one will be...

  • I need this in java please Create an automobile class that will be used by a...

    I need this in java please Create an automobile class that will be used by a dealership as a vehicle inventory program. The following attributes should be present in your automobile class: private string make private string model private string color private int year private int mileage. Your program should have appropriate methods such as: default constructor parameterized constructor add a new vehicle method list vehicle information (return string array) remove a vehicle method update vehicle attributes method. All methods...

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • Create a UML diagram to help design the class described in exercise 3 below. Do this...

    Create a UML diagram to help design the class described in exercise 3 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Baby class object; should they be private or public? Determine what class methods are required; should they be private or public? 3. Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two...

  • please need help with the java program A micro car rental company. package yourLastName.cis4110.assignment5; Please create...

    please need help with the java program A micro car rental company. package yourLastName.cis4110.assignment5; Please create an enum type, say Cartype, featuring all rental car types -- SUBCOMPACT, COMPACT, MIDSIZE, FULLSIZE (You may put the enum definition in Car class) class Car Instance variables: - private String plateNum - private Cartype carType - private boolean availability Member methods:             - Please define all setters and getters class RentACar - This is a five car system for prototyping instance variables:...

  • JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free...

    JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free to change the code below to fit the task better. thanks :) Assigment Create a new version of GenericStack (Have started on the code below) that uses an array instead of an ArrayList (this version should also be generic). Be sure to check the size of the array before adding a new item; - if the array becomes full, double the size of the...

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