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

CSci 111 – computer Science I Fall 2014 Cynthia Zickos

Embed Size (px)

DESCRIPTION

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: - PowerPoint PPT Presentation

Citation preview

Page 1: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

CSci 111 – computer Science IFall 2014Cynthia ZickosWRITING A SIMPLE PROGRAM IN JAVA

Page 2: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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.

Page 3: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

Example: Class name of VacationMiles…

public class VacationMiles

{

public static void main (String [] args){

}

}

Page 4: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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.

Page 5: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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

{

}

}

Page 6: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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.

Page 7: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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)

Page 8: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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.

Page 9: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

New version after incorporating this into our program…

import java.util.Scanner;

public class VacationMiles

{

public static void main (String [] args){

String familyName; // inputint milesTraveled; // inputdouble average; // outputint totalMiles = 0; // accumulator variable must also be initialized.

}

}

Page 10: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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.

Page 11: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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

Page 12: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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.

Page 13: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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

Page 14: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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

Page 15: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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 statementsTo Java :

System.out.println(“The “ + familyName + “ traveled “ + milesTraveled + “on their vacation. “);

Page 16: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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;

Page 17: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

Now we have. Import java.util.Scanner;

public class VacationMiles

{

public static void main (String [] args){ Scanner scan = new Scanner (System.in);

String familyName; // inputint milesTraveled; // inputdouble average; // outputint 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 inputSystem.out.print(“enter the miles “);milesTraveled = scan.nextInt();System.out.println(“The “ + familyName + “ traveled “ + milesTraveled + “on their vacation. “);

totalMiles += milesTraveled; }

}

Page 18: CSci  111 – computer Science I Fall 2014 Cynthia Zickos

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.