Question

In: Computer Science

in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...

in netbeans using Java

Create a project and a class with a main method, TestCollectors.

☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set.

☑ Add a method addToCollection. In this method, add one to collected and report the new value. If after this we have collected all the available items, print a congratulatory message.

☑ Write a toString for this class as follows:

If there are more objects collected than are available, the result should be something like

an overflowing collection with 20 out of 15 items

if there are exactly the same number collected as available

a perfect collection with 15 out of 15 items

if there are fewer collected than are available, it depends whether we are being completist, so either

a sadly incomplete collection with 10 out of 15 items

or

a collection with 10 out of 15 items

☑ [EC +10] Actually, if the number available is 0, no matter how many are collected, the result should be

not really a collection at all

adjust your toString accordingly

☑ In your main, create several Collectors with different values and check that their toStrings come out correctly

Solutions

Expert Solution

//PART-A

//class
class Collector
{
//3 instance variable
int collected;
int available;
boolean completist;
//constructor to set collected, available, completist value for the collector class object
public Collector(int c,int a ,boolean comp)
{
//setting 2 instnace variable to the respective parameters
collected=c;
available=a;
completist=comp;
}
//function addTo Collection
void addToCollection()
{
//add 1 to collected
collected=collected+1;
//if collected= available
if(collected==available)
{
//print conratulatory message
System.out.println("Congratulation, you have collected all available items");
}

}
//toString method
public String toString()
{
//if available is 0 then
if(available==0)
return "not really a collection at all";//return this string
//If there are more objects collected than are available
else if (collected>available)
return "an overflowing collection with "+collected+" out of "+available+" items";//return this string

//if there are exactly the same number collected as available
else if (collected==available)
return "a perfect collection with "+collected+" out of "+available+" items";//return this string
//if there are fewer collected than are available
else
{
//it depends whether we are being completistor not
//if completist then
if (completist==true)
return "a sadly incomplete collection with "+collected+" out of "+available+" items";//return this string
//otherwise
else
return "a collection with "+collected+" out of "+available+" items";//return this string
}
}
}
//main class to test above class
class TestCollectors
{
public static void main(String[] args)
{
//1st object c1 with parameters passed as given
Collector c1=new Collector(0,3,true);
//calling addToCollection 2 times
c1.addToCollection();
c1.addToCollection();
//here it will call the toString method and appropriate string will be returned and printed
System.out.println(c1);
//2nd object c2 with parameters passed as given
Collector c2=new Collector(3,0,true);
//calling addToCollection 1 time
c2.addToCollection();
//here it will call the toString method and appropriate string will be returned and printed
System.out.println(c2);
//3rd object c3 with parameters passed as given
Collector c3=new Collector(0,1,true);
//calling addToCollection 2 times
c3.addToCollection();
c3.addToCollection();
//here it will call the toString method and appropriate string will be returned and printed
System.out.println(c3);
//4th object c4 with parameters passed as given
Collector c4=new Collector(1,2,false);
//calling addToCollection 1 time
c4.addToCollection();
//here it will call the toString method and appropriate string will be returned and printed
System.out.println(c4);
//5th object c5 with parameters passed as given
Collector c5=new Collector(1,2,false);
//here it will call the toString method and appropriate string will be returned and printed
System.out.println(c5);

}//main ends
}//class ends

//NEXT PART

//importing package which has Scanner class
import java.util.*;
//class
class sort
{
//main method
public static void main(String[] args)
{
//declaring 3 variables
int x,y,z;
//creating Scanner class object sc
Scanner sc=new Scanner(System.in);
System.out.print("Enter 3 integer values: ");
//taking 3 integer inputs in x,y and z
x=sc.nextInt();
y=sc.nextInt();
z=sc.nextInt();
//declaring 3 extra variables
int large;
int small;
int med;
//code for finding large value out of x,y and z
//if x> y then
if(x>y)
{
//if x >z
if(x>z)
large=x;//set large to x
//otherwise
else
large=z;//set large to z
}
//otherwise(if y>x) then
else
{
//if y> z then
if(y>z)
large=y;//set large to y
//otherwise
else
large=z;//set large to z
}


//if x is equal to large
if(x==large)
{
//if y>z
if(y>z)
{
small=z;//set small to z
med=y;//set med to y
}
//otherwise( if z>y)
else
{
small=y;//set small to y
med=z;//set med to z
}
}

//if y is equal to large
else if (y==large)
{
//if x>z
if(x>z)
{
small=z;//set small to z
med=x;//set med to x
}
//otherwise (if z>x)
else
{
small=x;//set small to x
med=z;//set med to z
}
}
//otherwise ( if z is equal to large)
else
{
//if x>y
if(x>y)
{
small=y;//set small to y
med=x;//set med to x
}
//otherwise (if y>x)
else
{
small=x;//set small to x
med=y;//set med to y
}
}
//printing the value of large and med and small with a space between them
System.out.println(large+" "+med+" "+small);
}
}

i hope it helps..

If you have any doubts please comment and please don't dislike.

PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME


Related Solutions

in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
Create a Netbeans project with a Java main class. (It does not matter what you name...
Create a Netbeans project with a Java main class. (It does not matter what you name your project or class.) Your Java main class will have a main method and a method named "subtractTwoNumbers()". Your subtractTwoNumbers() method should require three integers to be sent from the main method. The second and third numbers should be subtracted from the first number. The answer should be returned to the main method and then displayed on the screen. Your main() method will prove...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read and follow the instructions below, placing the code statements needed just after each instruction. Leave the instructions as given in the code. Declare and create (instantiate) an integer array called List1 that holds 10 places that have the values: 1,3,4,5,2,6,8,9,2,7. Declare and create (instantiate) integer arrays List2 and List3 that can hold 10 values. Write a method called toDisplay which will display the contents...
create a project and in it a class with a main. We will be using the...
create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?"); int w = scan.nextInt();...
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder. 3. Import Scanner and Stacks from the java.util package. 4. Create a Stack object named basket. 5. The output shall: 5.1.Ask the user to input the number of fruits s/he would like to catch. 5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava. 5.3.Display all the fruits that the...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT