CSci 111 – computer Science I Fall 2014 Cynthia Zickos

Download Report

Transcript CSci 111 – computer Science I Fall 2014 Cynthia Zickos

CSci 111 – computer
Science I
Fall 2014
Cynthia Zickos
WRITING A SIMPLE PROGRAM IN JAVA
Choose a name for the class
Use the name to set up the framework for your program.
Example:
Write a program that will interactively read a series of 5 family names and the number of miles
traveled on vacation for each of the families. The program should output the family name and
number of miles traveled as they are read in and the average number of miles traveled by the
families as a group at the end of the program.
Choose a name and using that name, set up a class heading and the framework for method
main.
Save in a file using the name of the class as the filename proper and “.java” as the extention.
Example: Class name of VacationMiles…
public class VacationMiles
{
public static void main (String [] args)
{
}
}
Next Step…
Will the program need any external code?
For example: This program will use some of the methods defined by the Scanner class to
interactively read the inputs.
Where is that code located?
The Scanner class is a part of the java.util package.
Add an import statement above the class heading.
New version…
import java.util.Scanner;
public class VacationMiles
{
public static void main (String [] args)
{
}
}
Declare the memory cells needed by the
program…
What memory cells are needed by this program?
What type of data will be stored in each memory cell? Whole number? Floating point number?
Text?
A good starting point for this is to identify any data that is read in by the program and any data
that results from the computations of the program.
Example:
Input(s) :
Family name – text (choose String)
Miles traveled – whole number (choose int)
Output(s):
Family name
Miles traveled
Average Miles traveled – floating point (choose double)
Data needed for intermediate results?
The formula for the Average is total miles traveled / number of families which means that a memory
cell for the total miles is also needed.
Total Miles – whole number (int)
Declaration statements
Now we have enough information to write the code for the declaration statements.
String familyName;
// input
int milesTraveled;
// input
double average;
// output
int totalMiles = 0;
// accumulator variable must also be initialized.
New version after incorporating this into
our program…
import java.util.Scanner;
public class VacationMiles
{
public static void main (String [] args)
{
String familyName; // input
int milesTraveled;
// input
double average;
// output
int totalMiles = 0;
// accumulator variable must also be initialized.
}
}
Write the logic for the program
(algorithm)
Now we have “started out program”.
We should write the sequence of steps that are needed to perform the basic task of the
program.
To begin, we can write these steps in pseudocode, a simple English-line code that frees us think
solely about the logic without addressing any specific syntax issues at this point.
The algorithm
Read in the family name and miles traveled for family 1
Output the family name and miles traveled
Add the miles traveled to the accumulator.
Read in the family name and miles traveled for family 2
Output the family name and miles traveled
Add the miles traveled to the accumulator.
Read in the family name and miles traveled for family 3
Output the family name and miles traveled
Add the miles traveled to the accumulator.
Calculate the average miles traveled
Output the average miles traveled
Convert each step of the algorithm into a
java statement…
Let’s start with a logical segment of the pseudocode- processing the first family. That consists
of:
1.
Read in the family name and miles traveled for family 1
2.
Output the family name and miles traveled
3.
Add the miles traveled to the accumulator.
Convert each step of the algorithm into a
java statement…
Read in the family name and miles traveled for family 1
◦ We will need 4 java statements for this one statement in pseudocode– a prompt and read for each of
the two inputs.
Output the family name and miles traveled converts to (1 or 2) println statements
Add the miles traveled to the accumulator converts to a “calculate and store” statement
Convert each step of the algorithm into a
java statement…
Let’s start with the reading of the inputs section.
Read in the family name and miles traveled for family 1
◦ We will need 4 java statements for this one statement in pseudocode– a prompt and read for each of the two inputs.
Wait! Before we can use any methods of the Scanner class as we planned, we must first create a Scanner object, as in:
Scanner scan = new Scanner(System.in);
Now! The prompt & reads can be coded.
System.out.print(“Enter the family name “);
// prompt the user
familyName = scan.nextLine();
// read (and store) the user’s input
System.out.print(“enter the miles “);
milesTraveled = scan.nextInt();
Convert each step of the algorithm into a
java statement…
Now we convert:
◦ Output the family name and miles traveled converts to 1 or 2 statements
To Java :
System.out.println(“The “ + familyName + “ traveled “ + milesTraveled + “on their vacation. “);
Convert each step of the algorithm into a
java statement…
Now we convert:
◦ Add the miles traveled to the accumulator converts to 1 statement
To Java as:
totalMiles += milesTraveled;
Now we have.
Import java.util.Scanner;
public class VacationMiles
{
public static void main (String [] args)
{
Scanner scan = new Scanner (System.in);
String familyName;
// input
int milesTraveled;
// input
double average;
// output
int totalMiles = 0;
// accumulator variable must also be initialized.
System.out.print(“Enter the family name “);
familyName = scan.nextLine();
System.out.print(“enter the miles “);
// prompt the user
// read (and store) the user’s input
milesTraveled = scan.nextInt();
System.out.println(“The “ + familyName + “ traveled “ + milesTraveled + “on their vacation. “);
totalMiles += milesTraveled;
}
Stop & test a bit..
Before we proceed coding the rest of the program, let’s make sure we have it right so far.
1.
Compile the program until it is syntactically correct.
2.
Plan the test data set.
3.
Execute the code.