In: Computer Science
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.
//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);
}
}