Today, we will continue programming in java. There are three problems that you will need to
solve.
- Finish the simple sorting program, using arrays.
In the previous classes we have already designed an algorithm to solve the problem, written the
algorithm in pseudo code, and even translated the pseudo code into java. However, because we used
variables, our program was a little inefficient and required a switch function to translate i-th
number into our declared variables. In this assignment you are asked to solve the problem using
arrays instead of variables. Here are a few requirements:
- Use of arrays: Initially, use an array of size 4 (just like we used 4 variables)
- Test the program with the following numbers, i.e. assign these values to your arrays.
input: {4,3,2,1}, {9,10,-4,9}
- Increase the size of the array to 20 and modify your program accordingly(You will definitely
need to increase the loop size). Check if your program is running correctly.
Here is an unfinished code for the sorting problem with variables.:
int a,b,c,d;
a = 10;
b = 13;
c = 12;
d = 9;
for(int counter=0;counter<4;counter++) {
int i = 1;
while(i<4) {
switch(i) {
case(1):
if(a>b) {
int temp = a;
a = b;
b = temp;
}break;
case(2): . . .
}
i++;
}
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
- Implement a user prompt program
In this problem, you are asked to implement a program that asks user login id and password. If
the user inputs login id and password incorrectly, she would have 2 more retries. If after three
tries the user cannot enter the password correctly, the program would terminate.
As this program uses several commands that we haven't covered in class, I will provide you with
the code. However, the program that is provided to you works only for one user -- "asic". Your task is to modify the code so that the program stores login id's and passwords of
multiple users, using maps or arrays. Here is the code for the user prompt problem. In case you are using maps, please refer
to Lecture 10, to see how you search entries in a map.
- 10 coins
Implement a program that advises you(or the user of the program) to win the game called 10 coins.
Here are the rules of 10 coins:
- You and a friend have a stack of 10 coins
- On each person's turn, they remove either 1 or 2 coins from the stack
- The person who removes the last coin wins
As you probably know, we have already designed an algorithm to solve this problem in Lecture 6. So your task
would be to translate the algorithm into java.
As input(from keyboard), your program should receive the number of coins currently present in the
stack
As an output, your program should return the number of coins the user should remove from the
stack.For example:
input: 1
output: 1
input:2
output:2
input: 4
output:1
To input the number of coins from keyboard use the following code:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();
int input_number = Integer.parseInt(input);