Programming Session ( Assignment 4 Programming part)

5 points

Assigned: Thursday, July 5
Due: Thursday, July 12.  

Demo by yourself. After demo, send the source code to me through email. 

Grading:

The code can be compiled and can at least pass the example testing cases(80%).    

Good style: meaningful names, proper indentation and comments. (20%).

 

1. [0.5 point] Write a program to ask a user to input a positive integer N. Then the program compute the sum of the first N positive integers: sum = 1+2+3+...+(N-1)+N.    

    (a) use while() {   } structure.

    (b) use for structure

    (c) use  do while structure.

     For example: 

     Input:    6

     Output:

                while loop: 21

                for loop: 21

                do while loop: 21

 2. [0.5 point] [The algorithm] Write a program to ask a user to input a positive integer N. The program output the pattern with N rows.    (reference: http://www.cs.unc.edu/~zhangjd/comp110/hw/inc3/AsteriskPattern.java)           

     For example

     Input:  7

 

     Output:     

3. [1 point] [The algorithm] Build a file to save student's score. The program will continually popup a dialog to ask a user to input the name and the score of a student. The user input is a single string containing the name and the score separated by space. The name is a single word. The score is an positive integer. If the user input is invalid (the name is not a single token or the score is not a positive integer string), the program will print out the error message. If the input is valid, the name and the score are saved to a file named score.txt. If the user input is "exit", the program output the number of inputted name and the average score.

    A positive integer string is composed solely by character "0", "1", ..."9" and the first character should not be "0". 

   (Reference: http://www.cs.unc.edu/~zhangjd/comp110/hw/inc27/FileGUI.java.txt, http://www.cs.unc.edu/~zhangjd/comp110/hw/inc28/Tokenize.java.txt, http://www.cs.unc.edu/~zhangjd/comp110/hw/inc28/ToLower.java)

   For example:

   Input:  Mary  75     Output: Mary  is saved to the file..

   Input:  John               Output: Error. Must have two tokens: name score

   Input:  John 75 A      Output: Error. Must have two tokens: name score

   Input:  John  kkk       Output: Error. The score must be an positive integer.

   Input:  John  0           Output: Error. The score must be an positive integer.    

   Input:  John 65          Output: John is saved to the file.

   Input:  Jack 55          Output: Jack  is saved to the file.

   Input: exit                  Output: The number of the input names is 3, average score 65.

         score.txt

 

4. [1 point] [The algirhtm] Search in the file. The program will continually popup a dialog to ask for a user to input a name. The program will search the score.txt. If the name is in the file, output the corresponding score and the grade. Otherwise, tell the user the name is not in the file. If the user input is "exit", the program will terminate.

    The relation of the score and the grade:

   

Grade Score
A score >= 90
B 90 > score >=  80
C 80 > score >= 70
D 70 > score >=60
F score < 60

    (Reference: http://www.cs.unc.edu/~zhangjd/comp110/hw/inc2/AverageFile.java)

    Input: Mary               Output: Mary's score is 75. The grade is  C.

    Input: Jack                 Output: Jack's score is 55. The grade is F.

    Input: Jane                 Output: Cannot find Jane in the file.      

 

5. [1 point] [The algirhtm] The program will ask for two strings str1 and str2. Then the program will count the number of the substring str2 contained in str1. (use charAt to access the characters in a string)

    For example:

    str1 = "hello world";   str2 = "hello".   The number of the substring str2 contained in str1 is 1;

    str1 = "a b a b";   str2 = "a b".   The number of the substring str2 contained in str1 is 2;

    str1 = "aaaaa";   str2 = "aaa".   The number of the substring str2 contained in str1 is 3;

    str1 = "bbbab"; str2 = "cc". The number of the substring str2 contained in str1 is 0;

6. [1 point] Following is the code to input three numbers. Please finish the code to output the numbers in ascending order. No additional variables should be used in the code.

import java.util.*;

public class ordering
{
     static Scanner console = new Scanner(System.in);

     public static void main(String[] args)
     {
          int a,b,c;

          // User prompt
          System.out.println("Give me three integers : ");

          // Three inputs
          a = console.nextInt();
          b = console.nextInt();
          c = console.nextInt();

     }
}

 

(hint: use nested if else:

  if (a < b)

  {

         if (c < a){System.out.println("The answer:" + c + " " + a + " " + b );}

             else if (c >b){..}

                ...

   }

   else{

          ...

   }

)