Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.

Similar presentations


Presentation on theme: "CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA."— Presentation transcript:

1 CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA

2 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.

3 Example: Class name of VacationMiles… public class VacationMiles { public static void main (String [] args) { }

4 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.

5 New version… import java.util.Scanner; public class VacationMiles { public static void main (String [] args) { }

6 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.

7 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)

8 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.

9 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. }

10 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.

11 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

12 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.

13 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

14 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();

15 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. “);

16 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;

17 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 “); // prompt the user familyName = scan.nextLine(); // read (and store) the user’s input System.out.print(“enter the miles “); milesTraveled = scan.nextInt(); System.out.println(“The “ + familyName + “ traveled “ + milesTraveled + “on their vacation. “); totalMiles += milesTraveled; }

18 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.


Download ppt "CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA."

Similar presentations


Ads by Google