java method already definedString fruitsnew StringSIZE canno
java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of
errors in these two .javas:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;
import java.util.StringTokenizer;
public class GroupProject3
{
public static void main(String[] args)
{
//declare variables
int index=0;
int counter=0;
String userChoice;
String option=\"\";
int listChoice;
int boxChoice;
//initial size of box
int PRODUCE_SIZE=5;
//An array of string to store the fruits from text file
String[] fruits=new String[SIZE];
//Create an instance of Scanner class to read input from keyboard
Scanner inputKeyboard=new Scanner(System.in);
//Declare a Scanner class object to read text file
Scanner fileReader=null;
//to store a fruit type from text file
String item=null;
//create a string array to store 3 randomly generated fruits
Produce[] randomFruits=new Produce[3];
//Try catch block to handle file not found exception
try
{
//open file to read fruits types
fileReader=new Scanner(new FileInputStream(\"fruits.txt\"));
}
catch (FileNotFoundException e)
{
//exits if file not found
System.out.println(\"File not found\");
System.exit(0);
}
//create two variables of Fruit and Vegetable classes
Fruit fruit=null;
Vegetable vegetable=null;
//Reads names of the fruits
while(fileReader.hasNext())
{
//read each new line of text
item=fileReader.nextLine();
//create a string tokenizer to split the line of Fruits
StringTokenizer delimeter=new StringTokenizer(item, \",\");
String itemName=delimeter.nextToken();
String type=delimeter.nextToken();
//check if the type is Fruit then create an object Fruit class and add to the produceItems array list
//otherwise create an instance of a Vegetable class object and add it to the produceItems list
if(type.equalsIgnoreCase(\"Fruit\"))
{
fruit=new Fruit(itemName,\"Fruit\");
produceItems[counter]=fruit;
}
if(type.equalsIgnoreCase(\"Vegetable\"))
{
vegetable=new Vegetable(itemName,\"Vegetable\");
produceItems[counter]=vegetable;
}
//increment counter
counter++;
}
System.out.println(\"Create a Box of Produce? y or Y for yes or n for No\");
userChoice=inputKeyboard.next();
//create null valued object for the BoxOfProduce class
BoxOfProduce boxOfProduce=null;
boolean stopReading=true;
//continue reading user choice to create BoxOfProduce object and randomly fill fruits in the produceItems array
while((userChoice.charAt(0)==\'y\'||userChoice.charAt(0)==\'Y\')&&stopReading)
{
//create an instance of Random class
Random random=new Random();
//select three fruits from the array and store in the string array
randomFruits[0]=produceItems[random.nextInt(PRODUCE_SIZE)];
randomFruits[1]=produceItems[random.nextInt(PRODUCE_SIZE)];
randomFruits[2]=produceItems[random.nextInt(PRODUCE_SIZE)];
//create an instance of BoxOfProduce class with three fruit arguments that are selected randomly
boxOfProduce=new BoxOfProduce(randomFruits);
//display selected fruits object
System.out.println(\"Box of 3 random fruits\");
System.out.println(boxOfProduce.toString());
//add reading the fruits of string type to the box up to maximum size
do
{
System.out.println(\"Do you want to add one more fruit to box? y or Y for yes or n for No\");
String choice=inputKeyboard.next();
if(choice.charAt(0)==\'y\'||choice.charAt(0)==\'Y\')
{
System.out.println(\"Enter fruit name\");
String fruitName=inputKeyboard.next();
//call the method add with fruitname
boxOfProduce.add(new Produce(fruitName,\"Fruit\"));
}
System.out.println(\"Do you want to continue: y or Y for yes or n for No\");
option=inputKeyboard.next();
}
while(!option.equalsIgnoreCase(\"n\"));
System.out.println(boxOfProduce.toString());
System.out.println(\"Avaliable Fruit\");
//Display the list of fruits avaliable to replace box object
for (int position=0; position System.out.println(\"Enter your substitue choice(Only One Fruit)\");
//get user choice
listChoice=inputKeyboard.nextInt();
System.out.println(\"Replace with \");
System.out.println(boxOfProduce.toString());
System.out.println(\"Select 1\"+\"to \"+boxOfProduce.size());
//get user choice
boxChoice=inputKeyboard.nextInt();
//check if valid
if(boxChoice>0 &&(boxChoice<=boxOfProduce.size()))
{
//Insert the fruit at the box at the given choice of box.
boxOfProduce.insertAt(boxChoice-1,
produceItems[listChoice-1]);
}
else
{
//If user made invalid choice then exit the program
System.out.println(\"Invalid choice\");
System.exit(0);
}
//Print newly substituted list of box.
System.out.println(\"New Box of Produce\");
System.out.println(boxOfProduce.toString());
System.out.println(\"Fruits Count :\"+
boxOfProduce.getFruitCount());
System.out.println(\"Vegetable Count :\"+
boxOfProduce.getVegetableCount());
//check if stopReading is not true then read user choice
//for to create next box of produce
if(stopReading)
{
System.out.println(\"Do you want to create a Box of Produce :y or Y for yes/ n for No\");
userChoice=inputKeyboard.next();
}
}
}
}
BoxOfProduce.java
public class BoxOfProduce
{
//set box max size to 20
//Box of maximum size
private final int MAX_SIZE=20;
//declare variables
int size;
private Produce[] fruitType=new Produce[MAX_SIZE];
//Constructor to set the type of fruits.
public BoxOfProduce(Produce[] fruits)
{
//count of box size
size=0;
//call setter methods to set fruits
setFirstFruit(fruits[0]);
setSecondFruit(fruits[1]);
setThirdFruit(fruits[2]);
}
//Add fruit to the string array of fruits
public void add(Produce fruit)
{
if(this.size())
{
fruitType[size]=fruit;
size=size+1;
}
}
//accepts two variables to replace a existing fruit with new fruit
public void insertAt(int index,Produce fruit)
{
fruitType[index]=fruit;
}
//accepts a string type fruit and set to the firstFruitfield
public void setFruitfield(Produce firstFruitType)
{
fruitType[size]=firstFruitType;
size=size+1;
}
//accept a string type fruit and set to secondFruitfield
public void setFruitfield(Produce firstFruitType)
{
fruitType[size]=firstFruitType;
size=size+1;
}
//method that returns the current size of box
public int size()
{
return size;
}
// Override toString method to represent the fruits
//description as a string
public String toString()
{
String fruits=\"\";
//create a description of string of box of items
//that are added to the box
for (int index = 0; index < size; index++)
fruits+=fruitType[index].getName()+\"\ \";
//returns the string description.
return fruits;
}
// The method getFruitCount returns the number of fruits
//in the box
public int getFruitCount()
{
int fruitCount=0;
for (int index = 0; index < size(); index++)
{
if(fruitType[index].getType().equalsIgnoreCase(\"Fruit\"))
fruitCount++;
}
return fruitCount;
}
/* The method getFruitCount returns the number of fruits
in the box.*/
public int getVegetableCount()
{
int vegetableCount=0;
for (int index = 0; index < size(); index++)
{
if(fruitType[index].getType().equalsIgnoreCase(\"Vegetable\"))
vegetableCount++;
}
return vegetableCount;
}
}
the other .javas are probably fine
Produce.java
public class Produce
{
//instance string variable
private String name;
private String type;
//Default constructor
public Produce()
{
name=\"\";
type=\"\";
}
//Parameterized constructor that accepts a string
//and sets the string as the item name
public Produce(String name,String type)
{
this.name=name;
this.type=type;
}
//Method to set the name of the Produce item
public void setName(String name)
{
this.name=name;
}
//Method to return the name of the Produce
public String getName()
{
return name;
}
//Method to set the name of the Produce item
public void setType(String type)
{
this.type=type;
}
//Method to return the name of the Produce
public String getType()
{
return type;
}
//Override the toString method that returns
//the string description of produced item.
public String toString()
{
return \"Name : \"+name+\" Type: \"+type;
}
}
Vegetable.java
public class Vegetable extends Produce
{
/*Constructor that calls the super class Produce
class that sets the name and type of the fruit.*/
public Vegetable(String name, String type)
{
super(name, type);
}
/* The method toString calls the super class toString method
to display the Vegetable description.*/
public String toString()
{
return super.toString();
}
}
Fruit.java
public class Fruit extends Produce
{
/*Constructor that calls the super class Produce
class that sets the name and type of the fruit.*/
public Fruit(String name,String type)
{
super(name,type);
}
/*Calls the super class toString method
that displays the name and type of the Fruit*/
public String toString()
{
return super.toString();
}
}
Solution
Hi, I have resolved all error in BoxOfProduce.java and GroupProject3.java.
Please let me know in cass of any issue.
I can not run program because i do not have \"fruits.txt\" file.
############# BoxOfProduce.java #######################
public class BoxOfProduce
{
//set box max size to 20
//Box of maximum size
private final int MAX_SIZE=20;
//declare variables
int size;
private Produce[] fruitType=new Produce[MAX_SIZE];
//Constructor to set the type of fruits.
public BoxOfProduce(Produce[] fruits)
{
//count of box size
size=0;
//call setter methods to set fruits
setFruitfield(fruits[0]);
setFruitfield(fruits[1]);
setFruitfield(fruits[2]);
}
//Add fruit to the string array of fruits
public void add(Produce fruit)
{
if(this.size() < MAX_SIZE)
{
fruitType[size]=fruit;
size=size+1;
}
}
//accepts two variables to replace a existing fruit with new fruit
public void insertAt(int index,Produce fruit)
{
fruitType[index]=fruit;
}
//accepts a string type fruit and set to the fruitfield
public void setFruitfield(Produce fruitTypeField)
{
fruitType[size]=fruitTypeField;
size=size+1;
}
//method that returns the current size of box
public int size()
{
return size;
}
// Override toString method to represent the fruits
//description as a string
public String toString()
{
String fruits=\"\";
//create a description of string of box of items
//that are added to the box
for (int index = 0; index < size; index++)
fruits+=fruitType[index].getName()+\"\ \";
//returns the string description.
return fruits;
}
// The method getFruitCount returns the number of fruits
//in the box
public int getFruitCount()
{
int fruitCount=0;
for (int index = 0; index < size(); index++)
{
if(fruitType[index].getType().equalsIgnoreCase(\"Fruit\"))
fruitCount++;
}
return fruitCount;
}
/* The method getFruitCount returns the number of fruits
in the box.*/
public int getVegetableCount()
{
int vegetableCount=0;
for (int index = 0; index < size(); index++)
{
if(fruitType[index].getType().equalsIgnoreCase(\"Vegetable\"))
vegetableCount++;
}
return vegetableCount;
}
}
################ GroupProject3.java #########################
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;
import java.util.StringTokenizer;
public class GroupProject3
{
public static void main(String[] args)
{
//declare variables
int index=0;
int counter=0;
String userChoice;
String option=\"\";
int listChoice;
int boxChoice;
//initial size of box
int PRODUCE_SIZE=5;
//An array of string to store the fruits from text file
Produce[] produceItems=new Produce[PRODUCE_SIZE];
//Create an instance of Scanner class to read input from keyboard
Scanner inputKeyboard=new Scanner(System.in);
//Declare a Scanner class object to read text file
Scanner fileReader=null;
//to store a fruit type from text file
String item=null;
//create a string array to store 3 randomly generated fruits
Produce[] randomFruits=new Produce[3];
//Try catch block to handle file not found exception
try
{
//open file to read fruits types
fileReader=new Scanner(new FileInputStream(\"fruits.txt\"));
}
catch (FileNotFoundException e)
{
//exits if file not found
System.out.println(\"File not found\");
System.exit(0);
}
//create two variables of Fruit and Vegetable classes
Fruit fruit=null;
Vegetable vegetable=null;
//Reads names of the fruits
while(fileReader.hasNext())
{
//read each new line of text
item=fileReader.nextLine();
//create a string tokenizer to split the line of Fruits
StringTokenizer delimeter=new StringTokenizer(item, \",\");
String itemName=delimeter.nextToken();
String type=delimeter.nextToken();
//check if the type is Fruit then create an object Fruit class and add to the produceItems array list
//otherwise create an instance of a Vegetable class object and add it to the produceItems list
if(type.equalsIgnoreCase(\"Fruit\"))
{
fruit=new Fruit(itemName,\"Fruit\");
produceItems[counter]= fruit;
}
if(type.equalsIgnoreCase(\"Vegetable\"))
{
vegetable=new Vegetable(itemName,\"Vegetable\");
produceItems[counter]=vegetable;
}
//increment counter
counter++;
}
System.out.println(\"Create a Box of Produce? y or Y for yes or n for No\");
userChoice=inputKeyboard.next();
//create null valued object for the BoxOfProduce class
BoxOfProduce boxOfProduce=null;
boolean stopReading=true;
//continue reading user choice to create BoxOfProduce object and randomly fill fruits in the produceItems array
while((userChoice.charAt(0)==\'y\'||userChoice.charAt(0)==\'Y\')&&stopReading)
{
//create an instance of Random class
Random random=new Random();
//select three fruits from the array and store in the string array
randomFruits[0]=produceItems[random.nextInt(PRODUCE_SIZE)];
randomFruits[1]=produceItems[random.nextInt(PRODUCE_SIZE)];
randomFruits[2]=produceItems[random.nextInt(PRODUCE_SIZE)];
//create an instance of BoxOfProduce class with three fruit arguments that are selected randomly
boxOfProduce=new BoxOfProduce(randomFruits);
//display selected fruits object
System.out.println(\"Box of 3 random fruits\");
System.out.println(boxOfProduce.toString());
//add reading the fruits of string type to the box up to maximum size
do
{
System.out.println(\"Do you want to add one more fruit to box? y or Y for yes or n for No\");
String choice=inputKeyboard.next();
if(choice.charAt(0)==\'y\'||choice.charAt(0)==\'Y\')
{
System.out.println(\"Enter fruit name\");
String fruitName=inputKeyboard.next();
//call the method add with fruitname
boxOfProduce.add(new Produce(fruitName,\"Fruit\"));
}
System.out.println(\"Do you want to continue: y or Y for yes or n for No\");
option=inputKeyboard.next();
}
while(!option.equalsIgnoreCase(\"n\"));
System.out.println(boxOfProduce.toString());
System.out.println(\"Avaliable Fruit\");
//Display the list of fruits avaliable to replace box object
for (int position=0; position<randomFruits.length; position++){
System.out.println(\"Enter your substitue choice(Only One Fruit)\");
//get user choice
listChoice=inputKeyboard.nextInt();
System.out.println(\"Replace with \");
System.out.println(boxOfProduce.toString());
System.out.println(\"Select 1\"+\"to \"+boxOfProduce.size());
//get user choice
boxChoice=inputKeyboard.nextInt();
//check if valid
if(boxChoice>0 &&(boxChoice<=boxOfProduce.size()))
{
//Insert the fruit at the box at the given choice of box.
boxOfProduce.insertAt(boxChoice-1,
produceItems[listChoice-1]);
}
else
{
//If user made invalid choice then exit the program
System.out.println(\"Invalid choice\");
System.exit(0);
}
//Print newly substituted list of box.
System.out.println(\"New Box of Produce\");
System.out.println(boxOfProduce.toString());
System.out.println(\"Fruits Count :\"+
boxOfProduce.getFruitCount());
System.out.println(\"Vegetable Count :\"+
boxOfProduce.getVegetableCount());
//check if stopReading is not true then read user choice
//for to create next box of produce
if(stopReading)
{
System.out.println(\"Do you want to create a Box of Produce :y or Y for yes/ n for No\");
userChoice=inputKeyboard.next();
}
}
}
}
}
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-0.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-1.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-2.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-3.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-4.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-5.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-6.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-7.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-8.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-9.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-10.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-11.webp)
![java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre java method already defined/String[] fruits=new String[SIZE]; cannot be found in two .java files out of errors in these two .javas: import java.io.FileInputStre](/WebImages/20/java-method-already-definedstring-fruitsnew-stringsize-canno-1046161-1761544167-12.webp)