Question

In: Computer Science

Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...

Java Language

-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

[EC+20]

-Add a new (different from part A) class with a main. In main, ask the user for a value for each of three int variables x, y, and z.

-Using conditionals and nesting/else-if, print the three ints back out in decreasing order. (You can ignore the case where two numbers are the same)

So whether you were given 2, 1, 3, or 1,2,3 or 3,1,2, you'd print out 3, 2, 1.

You may add extra variables (i.e. small, med, and large) if you wish.

Make sure you test all possible orders for the three numbers.

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);
}
}


Related Solutions

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,...
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...
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?");...
Language: Java Create a TryIt.java program with a main method. You are going to use this...
Language: Java Create a TryIt.java program with a main method. You are going to use this program to demonstrate some things about how Java works. Make your methods here private, because they are not intended to be called from outside the program. For each test below, make sure that you print to the console: 1) What is being tested 2) The desired output 3) The actual output Question to be answered: Should you use == or the String method equals...
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...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT