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,...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment. public static int sumOfDigits(int n) This method returns the sum of all the digits. sumOfDigits(-34) -> 7 sumOfDigits(1038) -> 12 public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept of a domain name, assuming a domain name has a single attribute: the domain name itself. Include the following: - Constructor: accepts the domain name as an argument. - getDomain: an accessor method for the domain name field. - setDomain: a mutator method for the domain name field. - prefix: a method returning whether or not the domain name starts with www. - extension:...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT