1 / 18

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:

Download Presentation

CSci 111 – computer Science I Fall 2014 Cynthia Zickos

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSci 111 – computer Science IFall 2014Cynthia 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 intmilesTraveled; // input double average; // output inttotalMiles = 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: • 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.

  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 intmilesTraveled; // input double average; // output inttotalMiles = 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. • Compile the program until it is syntactically correct. • Plan the test data set. • Execute the code.

More Related