Logout

Sample Class (to see what's possible)

As noted in the comments, but it's worth highlighting:

FOR THIS WHOLE PROGRAM REMEMBER THIS IS JUST TO SHOW YOU THE KINDS OF THINGS PROGRAMS CAN DO

YOU ARE NOT EXPECTED TO UNDERSTAND OR BE ABLE TO REMEMBER MUCH OF THIS AT ALL (At this point.)

Never-the-less, I will comment briefly on the various parts which I mentioned in class.

Croaker2.java
/Users/adelaide/Public/Netbeans - All JSR Projects/Croaker Copy2/src/croaker2package/Croaker2.java
 1 
 2 package croaker2package;
 3 
 4 import java.io.*;
 5 
 6 /**
 7  * @author Chi
 8  */
 9 public class Croaker2 {
10     
11     
12     //FOR THIS WHOLE PROGRAM REMEMBER THIS IS JUST TO SHOW YOU THE KINDS OF THINGS PROGRAMS CAN DO
13     //YOU ARE NOT EXPECTED TO UNDERSTAND OR BE ABLE TO REMEMBER MUCH OF THIS AT ALL.
14     //Never-the-less, I will comment briefly on the various parts which I mentioned in class.
15     
16     
17     public static void main(String[] args) {//The main method every program needs.
18         try {//Try will allow us to try this and if it causes a problem things won't freeze.
19             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20             //The above line will allow us to read in stuff from the console.
21             System.out.println("Type in cow, frog, or cat, and press Enter.");
22             //When Enter is presses, what was typed by the user will be read in:
23             String theAnimalInput = br.readLine();
24             System.out.println("The animal chosen was: " + theAnimalInput);
25             //We output what the user input.
26 
27             System.out.println("How many times would you like your chosen animal to make its sound? (Press Enter) when finished");
28             int i = Integer.parseInt(br.readLine());//This line converts what the user types in to be an integer.
29 
30             if (theAnimalInput.equals("cow")) {//Beginning of a conditional block
31                 int k = 0;
32                 while (k < i) {//While k is less than the number entered in, this loop repeats.
33                     //And eventually, k will not be less than i, since k goes up by 1 each loop. See below.***
34                     if(k == 0){//When k == 0, it's actually the "1 time", so we want the word time to be singular.
35                         System.out.println("Our cow says moo " + (k + 1) + " time.");
36                     }
37                     else{//For all the other times, in which case, time should be plural: "times".
38                         System.out.println("Our cow says moo " + (k + 1) + " times."); 
39                     }
40                     k++;//Where k goes up by one each loop.
41                 }
42             }
43             else if(theAnimalInput.equals("frog")) {//If not "cow" entered, but rather, "frog".
44                 int k = 0;
45                 while (k < i) {
46                     if(k == 0){
47                         System.out.println("Our frog says croak " + (k + 1) + " time.");
48                     }
49                     else{
50                         System.out.println("Our frog says croak " + (k + 1) + " times.");
51                     }
52                     k++;
53                 }
54             }
55             else if(theAnimalInput.equals("cat")) {//If neither "cow" nor "frog" entered, rather, "cat".
56                 int k = 0;
57                 while (k < i) {
58                     if(k == 0){
59                         System.out.println("Our cat says meow " + (k + 1) + " time.");
60                     }
61                     else{
62                         System.out.println("Our cat says meow " + (k + 1) + " times.");
63                     }
64                     k++;
65                 }
66             }
67             else{//For the case where the user did not enter in any of the animals he/she was supposed to.
68                 System.out.println("You did not enter either cow or frog or cat. Please try again.");
69             }
70 
71 
72         } catch (IOException ex) {//When the try block runs into an error, the program comes down here, so no freeze.
73             System.out.println("Error when reading croaker input JSR.");
74         }
75     }
76 }
77