Program1

There is no one correct answer to the process of producing a correct program from a description of the problem to be solved. You can approach it from the top..down, bottom.. up, or a combination of the 2 as we discussed in class. This is one approach. Again, here is the description of the problem to be solved:

Write a program to get 2 numbers from the user. The program will print out the biggest, the smallest, and the average of the 2 numbers. If the 2 numbers are equal, you will get a third number. Then the program will ask for 2more numbers and repeat until the first number is equal to 0. Then you will exit the loop and print the average of all the numbers entered and how many times the pair of numbers entered were the same.

First, copy the template header from the example Trialtest on the network or Lesson 3 on this web site. With the comments taken out, you should have the following:



import java.io.*;

public class TrialTest
{
public static void main( String args[] ) throws IOException
{
DataInputStream input;

input = new DataInputStream(System.in.);

}

}

The only thing that will change from Application to Application for you is the name of the class.. you could change TrialTest to any name you choose. Otherwise, it is a cut and paste. Next, declare the rest of your variables. You always will need an object of class type DataInputStream so you can get input. The examples in the text do not need this because they only allow for 1 character input. We want a bit more flexibility. We will need some new variables as well. Look at the program you are going to write. You must have 2 integer numbers. You may need a third. You also need to count how many times the first 2 numbers were the same. Lets add 4 integer variables to our code plus one for wait so we can see our output.. If we forget one, we can always add it later.. Let's also set all of these variables to 0.

import java.io.*;

public class TrialTest
{
public static void main( String args[] ) throws IOException
{
DataInputStream input;

input = new DataInputStream(System.in.);

int a, b, c, sameCount,wait;

a=b=c=sameCount=0;

}

}



Now we are ready to design the code. Write out a step by step description of how to solve the problem in pseudocode. Remember, you can only do one thing at a time, so think of exactly what you would have to do to solve the problem manually. Here is a copy of the problem followed by an example of the first step of pseudocode:

Write a program to get 2 numbers from the user. The program will print out the biggest, the smallest, and the average of the 2 numbers. If the 2 numbers are equal, you will get a third number. Then the program will ask for 2 more numbers and repeat until the first number is equal to 0. Then you will exit the loop and print the average of all the numbers entered and how many times the pair of numbers entered were the same.

Pseudocode:

get 2 numbers
if the first one is bigger
print the first is bigger
if the second one is bigger
print the second one is bigger
if they are the same
get a third number

We are now part way there.. 2 problems remain.. at the end we need to find the average and to count how many times the first 2 numbers were the same, so lets add this to our pseudocode:

get 2 numbers
add 2 to the total numbers counter
add both numbers to the sum
if the first one is bigger
print the first is bigger
if the second one is bigger
print the second one is bigger
if they are the same
add 1 to the counter for same numbers
get a third number
add 1 to the total numbers counter
add the third number to the sum


This is all we have to do for each time through the loop. Now we put the loop around it. Since we know that we do not know how many times we will do the loop ahead of time, we know it will be a sentinel value loop. The template for this is:

Get inputs

while (...........)

{



Get inputs

}

Now Apply the template to our pseudocode:

get 2 numbers

while (the first number is not 0)

{
add 2 to the total numbers counter
add both numbers to the sum
if the first one is bigger
print the first is bigger
if the second one is bigger
print the second one is bigger
if they are the same
add 1 to the counter for same numbers
get a third number
add 1 to the total numbers counter
add the third number to the sum

get 2 numbers
}

Finally, at the end of the loop we can print the average and how many pairs of numbers were the same:

get 2 numbers
while (the first number is not 0)
{
add 2 to the total numbers counter
add both numbers to the sum
if the first one is bigger
print the first is bigger
if the second one is bigger
print the second one is bigger
if they are the same
add 1 to the counter for same numbers
get a third number
add 1 to the total numbers counter
add the third number to the sum
}
print the average by dividing the sum by the total numbers
print the counter for the same numbers



Now, we just need to translate this into Java. get 2 numbers would be:

System.out.println( "Enter first number " );
a = Integer.parseInt(input.readLine());

System.out.println( "Enter second number " );
b = Integer.parseInt(input.readLine());



Below is the translated Java code. See if you can follow the translation from the pseudocode: Note that we needed to add a few more variable... sum and totalNums

import java.io.*;

public class Sample
{
public static void main( String args[] ) throws IOException
{
DataInputStream input;
int a, b, c, sameCount, sum, totalNums,wait;
a=b=c=sameCount=sum=totalNums=0;
input = new DataInputStream(System.in);

/* get 2 numbers */
System.out.println( "Enter first number " );
a = Integer.parseInt(input.readLine());
System.out.println( "Enter second number " );
b = Integer.parseInt(input.readLine());

/* while (the first number is not 0) */
while ( a != 0)
{
/*add 2 to the total numbers counter */
totalNums +=2;
/*add both numbers to the sum */
sum = sum + a + b;
/* if the first one is bigger */
if (a > b)
System.out.println("the first is bigger");

/* if the second one is bigger */
if (b > a)
System.out.println("the second one is bigger");

/*if they are the same */
if (a == b)
{
/*add 1 to the counter for same numbers*/
sameCount++; /* or totalEqual = totalEqual + 1;*/
/* get a third number */
System.out.println( "Enter third number " );
c = Integer.parseInt(input.readLine());

/* add 1 to the total numbers counter */
totalNums++;
/* add the third number to the sum */
sum = sum + c;
}

/* get 2 numbers */
System.out.println( "Enter first number " );
a = Integer.parseInt(input.readLine());
System.out.println( "Enter second number " );
b = Integer.parseInt(input.readLine());


} /* end of while loop */


/* print the average by dividing the sum by the total numbers */
System.out.println(sum/totalNums + " is the average");
/* print the counter for the same numbers */
System.out.println("The number of equal pairs was " + sameCount);
wait = System.in.read();

} /* end of main */

} /* end of class */

 

BuiltWithNOF

[Java at Evergreen] [Syllabus] [To Do List] [Lessons/Programs] [Evaluation/Review] [Help] [FAQ/GWIFO] [Discuss/Reports] [Resources] [Readings] [Portfolio] [Instructor]