Lab 4: Programming in java (loops, arrays, maps, input, output)

Assignment due on Thursday, June 5

Today, we will continue programming in java. There are three problems that you will need to solve.

  1. 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: 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);
    
    
  2. 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.

  3. 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: 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);
    

Submission: You should create three java files, one for each of the program. Name them "your name"_4_"number".java, where you will need to replace "your name" with your name and "number" with the number of the problem(1,2 or 3). Email me your solutions before the deadline.