Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.

Similar presentations


Presentation on theme: "Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010."— Presentation transcript:

1 Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

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

3 Assignment See my website for the new assignment.

4 Chapter Topics A Simple Java Program Output Comments Input Algorithms Errors and Warnings Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

5 Output Two commands System.out.println outputs newline character after the output System.out.print leaves 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 in a given number of hours 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. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

9 public class Flight { /* This program finds the distance a jet travels. */ public static void 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 Begins and ends with brace { … } Note declaration of main method Where the execution begins. public static void main(String[] args) Also begins and ends with brace { … } 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. o A variable is a memory location. timeHrs = 5; sets timeHrs to 5. int distance = timeHrs * speedMPH; o D oes the multiplication on the right first. o 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. I don't use it.

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

15 Try a Program Today is John’s birthday. Write a program that sets a variable to his birth year and another variable to the current year. Output his age today. Sample output: John is 21 today.  Before writing the program, write an algorithm. o Algorithm is instruction in English-like code, pseudocode.

16 Input

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

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(); Let’s change the Age program so that it asks the user for the current year and the birth year. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

19 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 touring. Start here Wed.

20 Login and Password If you don't have a login, send me email. Contact me if you forget your password. Change your password to something easy to remember.  ssh -Y onyx  Login again  passwd  Nothing shows while you are typing.

21 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. Write an algorithm with a partner Along with your partner, code the program using eclipse. – Print it and hand in a copy with both names on it.

22 Questions What type of object is used to handle input? What package does the Scanner belong to?

23 Steps Figure 1-5 Steps involved in developing a Java program Imagine! Java: Programming Concepts in Context by Frank M. Carrano, © Pearson Education – Prentice Hall, 2010

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

25 Errors and Warnings Error prevents program from compiling  Red x in margin Warning means something might be wrong.  Yellow icon in margin.  Try to get rid of it.

26 Questions What kind of error is it? 1.Eclipse shows a red x in the margin because a quotation isn’t closed. 2.A program to convert Fahrenheit to Celcius gives incorrect results. 3.Eclipse shows a yellow icon in the margin because a variable hasn’t been used yet.


Download ppt "Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010."

Similar presentations


Ads by Google