1 / 31

Introduction

Introduction. Chapter 1 8/23 & 8/24. Check zyBooks Completion. Click on the boxes for each section. Assignment. See my website for the new assignment. See Blackboard announcement for information on how to get zyBooks. Today's Topics. Parts of a Java Program Output Comments Input

aedwards
Download Presentation

Introduction

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. Introduction Chapter 1 8/23 & 8/24

  2. Check zyBooks Completion Click on the boxes for each section.

  3. Assignment • See my website for the new assignment. • See Blackboard announcement for information on how to get zyBooks.

  4. Today's Topics • Parts of a Java Program • Output • Comments • Input • Algorithms • Errors and Warnings

  5. Output • Two commands • System.out.printlnoutputs newline character after the output • System.out.printleaves the cursor on the same line. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

  6. Displaying Text System.out.print(“Black”); System.out.println(“bird”); System.out.println(“sings.”); Output? Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

  7. Comments • Documents the program • Comments • Begin line with double slash // • Ends with the end of the line. • Span multiple lines between slash-star combination./* . . . . . . */ Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

  8. A Simple Java Program • A program is needed to figure out how far a jet can travel given number of hours it travels and the speed at which it is flying. Write a program to set the time in whole hours and the rate of travel in MPH. Output the distance traveled. • The instructions in a program are called code. • Here is the code: Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

  9. publicclass Flight { /* This program finds the distance a jet travels. */ publicstaticvoid main(String[] args) { int timeHrs = 5; int speedMPH = 600; int distance = timeHrs * speedMPH; System.out.println(distance + " miles traveled."); }//end main method }//end Flight class

  10. A Simple Java Program • Note definition of a class • Block begins and ends with brace{ … } • main method • Where the execution begins. public static void main(String[] args) • Block begins and ends with brace{ … } • Statements within method end with a ; • Tell processor what to do. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

  11. Assignment Statement int timeHrs = 5; • int timeHrs declares a variable. • A variable is a memory location. • Memory is a place to store values temporarily. • timeHrs = 5; sets timeHrs to 5. • int distance = timeHrs * speedMPH; • Does the multiplication on the right first. • Assigns the answer to distance.

  12. Notes on Output • + operator in print statements • Add numbers • (5+6) • Concatenate Strings. • "to" + "day" --> "today" • 15 + " inches" --> 15 inches • Strings that span multiple lines Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

  13. return statement The return statement is optional in the main method. It stops the program. I don't usually use it.

  14. Questions • What is the difference between a print and a println? • Where do the statements in a Java program belong? • Give an example of an assignment statement.

  15. Input

  16. Packages

  17. Package name Class name Keyboard Input • Use Scanner class from Java Class Library • Must use import statement:import java.util.Scanner; • Next, createScannerobject Scanner keyboard = new Scanner(System.in); Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

  18. Keyboard Input • Keyboard is an object which can perform methods. • Use method to receive data from keyboard, store in variable • int x = keyboard.nextInt(); • Example: Age program Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

  19. Age Program Today is Andy's birthday. Write a program to input his previous age then output how old he is today.

  20. import java.util.Scanner; publicclass Birthday { publicstaticvoid main(String[] args) { //Input Previous Age Scanner scan = new Scanner(System.in); System.out.println("Andy's previous age?"); intage = scan.nextInt(); //Find his age today inttodayAge= age + 1; //Output his age today System.out.println("Andy is " + todayAge + " today."); }

  21. Find the Number of People Write a program to figure how many people are touring Yosemite Park today. Ask for the number of people one tour bus can hold and the number of buses in the park. Output the total number of passengers.

  22. import java.util.Scanner; publicclass Yosemite001 { publicstaticvoid main(String[] args) { //Input spots on bus and no. of buses Scanner scan = new Scanner(System.in); System.out.print("Spots on a bus?"); intspots = scan.nextInt(); System.out.println("Number of buses?"); intbuses = scan.nextInt(); //Find total number of people inttotal = spots * buses; //Output total number of people System.out.println(total + " people in the park."); scan.close(); } }

  23. Questions • What type of object is used to handle input? • What package does the object belong to?

  24. Number of Credits Earned Write a program to input the number of credits that a student earned during each of two semesters at Boise State. Add up the credits then output the total.

  25. Errors and Warnings August 29 & 30

  26. Errors and Warnings

  27. Steps in Developing • Figure 1-5 Steps involved in developing a Java program

  28. Errors • Syntax Error • Violation of programming language rules. • Caught by compiler. • Program will not compile. • Logic Error • Error while program runs. • For example, incorrect computation. distance = rate/time;

  29. Errors and Warnings • Error prevents program from compiling • Warning means something might be wrong. • Try to get rid of it.

  30. Questions What kind of error is it? 1.A program doesn't compile because a quotation isn’t closed. 2. A program to convert Fahrenheit to Celcius gives incorrect results. 3. The compiler complains because a variable hasn’t been used. The program still compiles.

  31. Participation • With your partner, correct program in zyLab 1.15. • I will come around and see your work. • Tell me what kinds of errors were in theprogram.

More Related