有谁做完了作业,教一下可以吗?
Q1. Assume that we want to write a program to simulate rolling of two dice. Write a main method for the Keyboard class below that prompts the user for two dice values each being between 1 and 6. If either of the two dice values is invalid, a message should appear indicating so but then the entered values should be ignored. This process should repeat for 5 times and then display in the console the number of times the roll of the dice was seven. Note that a seven can occur 6 ways as shown below.
public class Keyboard {
publicstaticint getInt() {
Scanner aScanner = new Scanner(System.in);
System.out.println("Enter a dice value between 1 and 6 :");
return(aScanner.nextInt());
}
}
Test your program with at least one test case and include the test results in out.txt file.
Q2. Write the method public int[] partialSort(int[] anArray) which takes an array of int as a parameter and returns a new array of int. The new array should contain at the front all the odd numbers from the given array and all the even numbers of that array at the end. For example, if we are given an array with the numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 then the resulting array should contain numbers as follows: 1, 3, 5, 7, 9, 2, 4, 6, 8, 10. Notice that all the odd numbers are in the first half of the array and the even numbers in the second half. Note however, that there may be no odd numbers or possibly no even numbers. You may assume that there is at least one number in the given array.
(Hint: you can use the modulus (%) operator to decide if something is odd/even).
Write the above method in a SortTester class with a public static void main() method that tests the partialSort() method withthe following arrays:
{1,2,3,4,5,6,7,8,9,10}
{8}
{5}
{9,7,5}
{98, 8}
{12, 57, 45, 9, 354, 3, 2, 56, 7,8 ,94, 45,67, 21}
Copy and paste your testing for marking. Your main method should prompt the user as follows:
Please enter number of elements in the array: 3
Please enter 1 element: 1
Please enter 2 element: 4
Please enter 3 element: 5
You entered the array: 1, 4, 5
Partially sorted as : 1, 5, 4
Note that you do not have to sort the input array.
Q3. Write a Month class with a public method called getName() and returns a String. This method would transform numbers 1, 2, 3, . . ., 12 into the corresponding month names January, February, March, . . ., December. Implement a class Month whose constructor parameter is the month number and whose getName () method returns the month name. Hint: Make a very long string "January February March . . .", in which you add spaces such that each month name has the same length. Then use substring to extract the month you want.
Test your class and method with the following tester and include the test results in out.txt file:
**
This program tests the Month class.
*/
public class MonthTester
{
public static void main(String[] args)
{
Month jan = new Month(1);
System.out.println("Name: " + jan.getName());
System.out.println("Expected: January");
Month mar = new Month(3);
System.out.println("Name: " + mar.getName());
System.out.println("Expected: March");
Month dec = new Month(12);
System.out.println("Name: " + dec.getName());
System.out.println("Expected: December");
}
}
Q4. Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>. Supply a method
void addCoin(String coinName)
Add a method toString to the Purse class that prints the coins in the purse in the format
Purse[Quarter,Dime,Nickel,Dime]
Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse p = new Purse();
p.addCoin("Quarter");
p.addCoin("Dime");
p.addCoin("Nickel");
p.addCoin("Dime");
System.out.println(p.toString());
System.out.println("Expected: Purse[Quarter,Dime,Nickel,Dime]");
}
}
Q5. Write a method reverse() that reverses the sequence of coins in a purse. Use the toString() method of the preceding question to test your code. For example, if reverse is called with a purse
Purse[Quarter,Dime,Nickel,Dime]
then the purse is changed to
Purse[Dime,Nickel,Dime,Quarter]
Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse p = new Purse();
p.addCoin("Quarter");
p.addCoin("Dime");
p.addCoin("Nickel");
p.addCoin("Dime");
System.out.println("Original purse: " + p.toString());
System.out.println("Expected: Purse[Quarter,Dime,Nickel,Dime]");
p.reverse();
System.out.println("Reversed purse: " + p.toString());
System.out.println("Expected: Purse[Dime,Nickel,Dime,Quarter]");
}
}
Q.6 Add a transfer() method to the previous purse class
public void transfer(Purse other)
that transfers the contents of one purse to another. For example, if a is
Purse[Quarter,Dime,Nickel,Dime]
and b is
Purse[Dime,Nickel]
then after the call a.transfer(b), a is
Purse[Quarter,Dime,Nickel,Dime,Dime,Nickel]
and b is empty.
Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
a.addCoin("Nickel");
a.addCoin("Dime");
Purse b = new Purse();
b.addCoin("Dime");
b.addCoin("Nickel");
a.transfer(b);
System.out.println(a.toString());
System.out.println("Expected: Purse[
Quarter,Dime,Nickel,Dime,Dime,Nickel]");
System.out.println(b.toString());
System.out.println("Expected: Purse[]");
}
}
Q7. Write a method for the Purse class
public boolean sameContents(Purse other)
that checks whether the other purse has the same coins in the same order.
Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
Purse b = new Purse();
b.addCoin("Quarter");
b.addCoin("Dime");
System.out.println(a.sameContents(b));
System.out.println("Expected: true");
Purse c = new Purse();
c.addCoin("Nickel");
c.addCoin("Dime");
c.addCoin("Nickel");
Purse d = new Purse();
d.addCoin("Nickel");
d.addCoin("Dime");
d.addCoin("Quarter");
System.out.println(c.sameContents(d));
System.out.println("Expected: false");
Purse e = new Purse();
e.addCoin("Nickel");
e.addCoin("Dime");
e.addCoin("Nickel");
Purse f = new Purse();
f.addCoin("Nickel");
f.addCoin("Dime");
System.out.println(e.sameContents(f));
System.out.println("Expected: false");
}
}
Q8. Write a method for the Purse class
public boolean sameCoins(Purse other)
that checks whether the other purse has the same coins, perhaps in a different order. For example, the purses
Purse[Quarter,Dime,Nickel,Dime]
and
Purse [Nickel,Dime,Dime,Quarter]
should be considered equal.
You will probably need one or more helper methods.Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
a.addCoin("Nickel");
a.addCoin("Dime");
Purse b = new Purse();
b.addCoin("Nickel");
b.addCoin("Dime");
b.addCoin("Dime");
b.addCoin("Quarter");
System.out.println(a.sameCoins(b));
System.out.println("Expected: true");
Purse c = new Purse();
c.addCoin("Quarter");
c.addCoin("Penny");
c.addCoin("Nickel");
c.addCoin("Dime");
Purse d = new Purse();
d.addCoin("Nickel");
d.addCoin("Dime");
d.addCoin("Dime");
d.addCoin("Quarter");
System.out.println(c.sameCoins(d));
System.out.println("Expected: false");
}
}
You will probably need one or more helper methods.Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
a.addCoin("Nickel");
a.addCoin("Dime");
Purse b = new Purse();
b.addCoin("Nickel");
b.addCoin("Dime");
b.addCoin("Dime");
b.addCoin("Quarter");
System.out.println(a.sameCoins(b));
System.out.println("Expected: true");
Purse c = new Purse();
c.addCoin("Quarter");
c.addCoin("Penny");
c.addCoin("Nickel");
c.addCoin("Dime");
Purse d = new Purse();
d.addCoin("Nickel");
d.addCoin("Dime");
d.addCoin("Dime");
d.addCoin("Quarter");
System.out.println(c.sameCoins(d));
System.out.println("Expected: false");
}
}
Q1. Assume that we want to write a program to simulate rolling of two dice. Write a main method for the Keyboard class below that prompts the user for two dice values each being between 1 and 6. If either of the two dice values is invalid, a message should appear indicating so but then the entered values should be ignored. This process should repeat for 5 times and then display in the console the number of times the roll of the dice was seven. Note that a seven can occur 6 ways as shown below.
public class Keyboard {
publicstaticint getInt() {
Scanner aScanner = new Scanner(System.in);
System.out.println("Enter a dice value between 1 and 6 :");
return(aScanner.nextInt());
}
}
Test your program with at least one test case and include the test results in out.txt file.
Q2. Write the method public int[] partialSort(int[] anArray) which takes an array of int as a parameter and returns a new array of int. The new array should contain at the front all the odd numbers from the given array and all the even numbers of that array at the end. For example, if we are given an array with the numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 then the resulting array should contain numbers as follows: 1, 3, 5, 7, 9, 2, 4, 6, 8, 10. Notice that all the odd numbers are in the first half of the array and the even numbers in the second half. Note however, that there may be no odd numbers or possibly no even numbers. You may assume that there is at least one number in the given array.
(Hint: you can use the modulus (%) operator to decide if something is odd/even).
Write the above method in a SortTester class with a public static void main() method that tests the partialSort() method withthe following arrays:
{1,2,3,4,5,6,7,8,9,10}
{8}
{5}
{9,7,5}
{98, 8}
{12, 57, 45, 9, 354, 3, 2, 56, 7,8 ,94, 45,67, 21}
Copy and paste your testing for marking. Your main method should prompt the user as follows:
Please enter number of elements in the array: 3
Please enter 1 element: 1
Please enter 2 element: 4
Please enter 3 element: 5
You entered the array: 1, 4, 5
Partially sorted as : 1, 5, 4
Note that you do not have to sort the input array.
Q3. Write a Month class with a public method called getName() and returns a String. This method would transform numbers 1, 2, 3, . . ., 12 into the corresponding month names January, February, March, . . ., December. Implement a class Month whose constructor parameter is the month number and whose getName () method returns the month name. Hint: Make a very long string "January February March . . .", in which you add spaces such that each month name has the same length. Then use substring to extract the month you want.
Test your class and method with the following tester and include the test results in out.txt file:
**
This program tests the Month class.
*/
public class MonthTester
{
public static void main(String[] args)
{
Month jan = new Month(1);
System.out.println("Name: " + jan.getName());
System.out.println("Expected: January");
Month mar = new Month(3);
System.out.println("Name: " + mar.getName());
System.out.println("Expected: March");
Month dec = new Month(12);
System.out.println("Name: " + dec.getName());
System.out.println("Expected: December");
}
}
Q4. Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>. Supply a method
void addCoin(String coinName)
Add a method toString to the Purse class that prints the coins in the purse in the format
Purse[Quarter,Dime,Nickel,Dime]
Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse p = new Purse();
p.addCoin("Quarter");
p.addCoin("Dime");
p.addCoin("Nickel");
p.addCoin("Dime");
System.out.println(p.toString());
System.out.println("Expected: Purse[Quarter,Dime,Nickel,Dime]");
}
}
Q5. Write a method reverse() that reverses the sequence of coins in a purse. Use the toString() method of the preceding question to test your code. For example, if reverse is called with a purse
Purse[Quarter,Dime,Nickel,Dime]
then the purse is changed to
Purse[Dime,Nickel,Dime,Quarter]
Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse p = new Purse();
p.addCoin("Quarter");
p.addCoin("Dime");
p.addCoin("Nickel");
p.addCoin("Dime");
System.out.println("Original purse: " + p.toString());
System.out.println("Expected: Purse[Quarter,Dime,Nickel,Dime]");
p.reverse();
System.out.println("Reversed purse: " + p.toString());
System.out.println("Expected: Purse[Dime,Nickel,Dime,Quarter]");
}
}
Q.6 Add a transfer() method to the previous purse class
public void transfer(Purse other)
that transfers the contents of one purse to another. For example, if a is
Purse[Quarter,Dime,Nickel,Dime]
and b is
Purse[Dime,Nickel]
then after the call a.transfer(b), a is
Purse[Quarter,Dime,Nickel,Dime,Dime,Nickel]
and b is empty.
Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
a.addCoin("Nickel");
a.addCoin("Dime");
Purse b = new Purse();
b.addCoin("Dime");
b.addCoin("Nickel");
a.transfer(b);
System.out.println(a.toString());
System.out.println("Expected: Purse[
Quarter,Dime,Nickel,Dime,Dime,Nickel]");
System.out.println(b.toString());
System.out.println("Expected: Purse[]");
}
}
Q7. Write a method for the Purse class
public boolean sameContents(Purse other)
that checks whether the other purse has the same coins in the same order.
Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
Purse b = new Purse();
b.addCoin("Quarter");
b.addCoin("Dime");
System.out.println(a.sameContents(b));
System.out.println("Expected: true");
Purse c = new Purse();
c.addCoin("Nickel");
c.addCoin("Dime");
c.addCoin("Nickel");
Purse d = new Purse();
d.addCoin("Nickel");
d.addCoin("Dime");
d.addCoin("Quarter");
System.out.println(c.sameContents(d));
System.out.println("Expected: false");
Purse e = new Purse();
e.addCoin("Nickel");
e.addCoin("Dime");
e.addCoin("Nickel");
Purse f = new Purse();
f.addCoin("Nickel");
f.addCoin("Dime");
System.out.println(e.sameContents(f));
System.out.println("Expected: false");
}
}
Q8. Write a method for the Purse class
public boolean sameCoins(Purse other)
that checks whether the other purse has the same coins, perhaps in a different order. For example, the purses
Purse[Quarter,Dime,Nickel,Dime]
and
Purse [Nickel,Dime,Dime,Quarter]
should be considered equal.
You will probably need one or more helper methods.Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
a.addCoin("Nickel");
a.addCoin("Dime");
Purse b = new Purse();
b.addCoin("Nickel");
b.addCoin("Dime");
b.addCoin("Dime");
b.addCoin("Quarter");
System.out.println(a.sameCoins(b));
System.out.println("Expected: true");
Purse c = new Purse();
c.addCoin("Quarter");
c.addCoin("Penny");
c.addCoin("Nickel");
c.addCoin("Dime");
Purse d = new Purse();
d.addCoin("Nickel");
d.addCoin("Dime");
d.addCoin("Dime");
d.addCoin("Quarter");
System.out.println(c.sameCoins(d));
System.out.println("Expected: false");
}
}
You will probably need one or more helper methods.Use the following class as your tester class:
/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
a.addCoin("Nickel");
a.addCoin("Dime");
Purse b = new Purse();
b.addCoin("Nickel");
b.addCoin("Dime");
b.addCoin("Dime");
b.addCoin("Quarter");
System.out.println(a.sameCoins(b));
System.out.println("Expected: true");
Purse c = new Purse();
c.addCoin("Quarter");
c.addCoin("Penny");
c.addCoin("Nickel");
c.addCoin("Dime");
Purse d = new Purse();
d.addCoin("Nickel");
d.addCoin("Dime");
d.addCoin("Dime");
d.addCoin("Quarter");
System.out.println(c.sameCoins(d));
System.out.println("Expected: false");
}
}