Introduction
Chapter 1
8/31 & 9/1
Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Check zyBooks Completion
Click on the boxes for each section.
Assignment• See my website for the new assignment.
Chapter Topics
A Simple Java ProgramOutputCommentsInputAlgorithmsErrors and Warnings
Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
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
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
Comments
Documents the programComments
• 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
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
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
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
Assignment Statementint 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 Does the multiplication on the right first.
o Assigns the answer to distance.
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
return statement
The return statement is optional in the main method. I don't use it.
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.
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.
Input
Keyboard InputUse 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 name Class name
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
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.
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.
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.
Questions
• What type of object is used to handle input?
• What package does the Scanner belong to?
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
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;
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.
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.