![Page 1: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/1.jpg)
Java basics
![Page 2: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/2.jpg)
Task Display the supposed forecast
I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943.
![Page 3: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/3.jpg)
Sample output
![Page 4: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/4.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}}
![Page 5: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/5.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} Three statements make up the action of method
main()
Method main() is part of class DisplayForecast
![Page 6: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/6.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} A method is a named piece of code that performs
some action or implements a behavior
![Page 7: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/7.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} An application program is required to have a
public static void method named main().
![Page 8: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/8.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} public, static, and void are keywords. They
cannot be used as names
public means the method is shareable
![Page 9: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/9.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} We will discuss static and void later
![Page 10: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/10.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} Java allows a statement to be made up of
multiple lines of text
Semicolons delimit one statement from the next
![Page 11: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/11.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} A class defines an object form. An object can
have methods and attributes
Keyword class indicates a class definition follows
![Page 12: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/12.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} A class like a method must have a name
![Page 13: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/13.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} Programs are read by people – make sure they are
readable.
Use whitespace, comments, and indentation to aid understanding
![Page 14: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/14.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} Whitespace separates program elements
Whitespace between program elements is ignored by Java
Whitespace
![Page 15: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/15.jpg)
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}} // indicates rest of the line is a comment
Comments are used to document authors, purpose, and program elements
Three comments
![Page 16: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/16.jpg)
Indentation
// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");
}}
Indentation indicates subcomponents
Method main() is part of DisplayForecast
Statements are part of method main()
![Page 17: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/17.jpg)
Method main()
public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");}
Class System supplies objects that can print and read values
System variable out references the standard printing object Known as the standard output stream
Variable out provides access to printing methods print(): displays a value println(): displays a value and moves cursor to the next
line
![Page 18: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/18.jpg)
System.out
+ println(String s) : void+ print(String s) : void+ ...
System.out : PrintStream
- destination =- ...
Variable System.out givesaccess to an output stream
of type PrintStream
The printing destination attributefor this PrintStream object is the
console window
The behaviors of a PrintStreamobject support a high-level view of
printing
![Page 19: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/19.jpg)
Selection
System . out . print ( "string" )
Literal character string that isthe parameter to print().
Member out of System is an outputstream object automatically
associated with the console windowrunning the application
Class System is definedin the standardpackage java.lang
The period indicates that we want to select anindividual class member of System
The period indicates that we want toselect an individual class member of out
Method member of out. The execution of member print()causes its parameter to be displayed to the output stream
![Page 20: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/20.jpg)
Method main()
public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");}
Method print() and println() both take a string parameter
The parameter specifies the value that is to be used in the invocation
![Page 21: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/21.jpg)
Method main()
public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");}
The print() statement starts the program output
I think there is a world market for
![Page 22: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/22.jpg)
Method main()
public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");}
The first println() statement completes the first line of output
I think there is a world market for maybe five computers
![Page 23: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/23.jpg)
Method main()
public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943.");}
The second println() statement starts and completes the second line of output
I think there is a world market for maybe five computers Thomas Watson, IBM, 1943
![Page 24: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/24.jpg)
Experiment
public static void main(String[] args) { System.out.print("The real problem is not whether ");
System.out.print("machines think but whether people "); System.out.println("do"); System.out.println(“-- B.F. Skinner (paraphrased)");}
What does this method main() output?
![Page 25: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/25.jpg)
Computation Programmers frequently write small programs for computing
useful things
Example – body mass index (BMI) Measure of fitness
Ratio of person’s weight to the square of the person’s height Weight in is kilograms, height is in meters
Person of interest is 4.5 feet and weighs 75.5 pounds
Metric conversions Kilograms per pound 0.454 Meters per foot 0.3046
![Page 26: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/26.jpg)
Common program elements Type
Set of values along with operators that can manipulate and create values from the set
Primitive types support numeric, character, logical values double and float
Values with decimals byte, short, int, long
Integers char
Characters (considered numeric) boolean
Logical values
Basic operators + addition - subtraction * multiplication / division
![Page 27: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/27.jpg)
Common program elements Constant
Symbolic name for memory location whose value does not change KILOGRAMS_PER_POUND
Variable Symbolic name for memory location whose value can
change weightInPounds
![Page 28: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/28.jpg)
Program outline for BMI.java// Purpose: Compute BMI for given weight and height public class BMI { // main(): application entry point public static void main(String[] args) { // define constants // set up person's characteristics // convert to metric equivalents // perform bmi calculation // display result } }
![Page 29: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/29.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds *
KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = metricWeight / (metricHeight * metricHeight); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + Math.round(bmi)); }
![Page 30: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/30.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds *
KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = metricWeight / (metricHeight * metricHeight); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + Math.round(bmi)); }
0.454KILOGRAMS_PER_POUND
![Page 31: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/31.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds *
KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = metricWeight / (metricHeight * metricHeight); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + Math.round(bmi)); }
0.3046METERS_PER_FOOT
![Page 32: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/32.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds *
KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = metricWeight / (metricHeight * metricHeight); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + Math.round(bmi)); }
75.5weightInPounds
![Page 33: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/33.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds *
KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = metricWeight / (metricHeight * metricHeight); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + Math.round(bmi)); }
4.5heightInFeet
![Page 34: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/34.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds *
KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = metricWeight / (metricHeight * metricHeight); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + Math.round(bmi)); }
34.2770metricWeight
![Page 35: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/35.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds *
KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = metricWeight / (metricHeight * metricHeight); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + Math.round(bmi)); }
1.3706metricHeight
![Page 36: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/36.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds *
KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = metricWeight / (metricHeight * metricHeight); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + Math.round(bmi)); }
18.2439bmi
![Page 37: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/37.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds *
KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = metricWeight / (metricHeight * metricHeight); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + Math.round(bmi)); }
Operator evaluation depend upon its operands
![Page 38: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/38.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds *
KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = metricWeight / (metricHeight * metricHeight); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + Math.round(bmi)); }
Math.round(bmi) is 18
![Page 39: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/39.jpg)
// Purpose: Convert a Celsius temperature to Fahrenheit
public class CelsiusToFahrenheit {
// main(): application entry pointpublic static void main(String[] args) { // set Celsius temperature of interest int celsius = 28;
// convert to Fahrenheit equivalent int fahrenheit = 32 + ((9 * celsius) / 5);
// display result System.out.println("Celsius temperature"); System.out.println(" " + celsius); System.out.println("equals Fahrenheit temperature");
System.out.println(" " + fahrenheit);}
}
![Page 40: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/40.jpg)
// Purpose: Demonstrate char arithmetic
public class LowerToUpper {
// main(): application entry pointpublic static void main(String[] args) { // set lower case character of interest char lowerCaseLetter = 'c';
// convert to uppercase equivalent char upperCaseLetter = 'A' + (lowerCaseLetter - 'a');
// display result System.out.println("Uppercase equivalent of"); System.out.println(" " + lowerCaseLetter); System.out.println("is"); System.out.println(" " + upperCaseLetter);}
}
![Page 41: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/41.jpg)
Expressions What is the value used to initialize expression
int expression = 4 + 2 * 5;
What value is displayed
System.out.println(5 / 2.0);
Java rules in a nutshell
Each operator has a precedence level and an associativity
Operators with higher precedence are done first
* and / have higher precedence than + and -
Associativity indicates how to handle ties
When floating-point is used the result is floating point
![Page 42: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/42.jpg)
Question Does the following statement compute the average of double
variables a, b, and c? Why
double average = a + b + c / 3.0;
![Page 43: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/43.jpg)
Interactive programs Programs that interact with their users through statements
performing input and output
BMI.java Not interactive – weight and height are fixed
![Page 44: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/44.jpg)
Support for interactive console programs Variable System.in
Associated with the standard input stream – the keyboard
Class BufferedReader Supports extraction of an input line as a character string
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
+ readln() : String+ ...
stdin : BufferedReader
- source =- ...
Variable stdin gives BufferedReaderaccess to an input stream
Input source attribute for thisBufferedReader is the keyboard
Behaviors of a BufferedReader supporthigh-level view of inputting text
![Page 45: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/45.jpg)
Accessing the standard input stream Set up
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
A new operation constructs a new object. The value of the operationis a reference to the new object. This new operation constructs a
BufferedReader object out of a new InputStreamReader object thatwas built using the object representing the standard input stream
new BufferedReader(new InputStreamReader(System.in))
![Page 46: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/46.jpg)
Interactive program for bmi Program outline
// Purpose: Compute BMI for user-specified// weight and height
import java.io.*;
public class BMICalculator {
// main(): application entry pointpublic static void main(String[] args)
throws IOException {// defining constants// displaying legend// set up input stream// get person's characteristics// convert to metric equivalents// perform bmi calculation// display result
}}
![Page 47: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/47.jpg)
public static void main(String[] args) throws IOException {final double KILOGRAMS_PER_POUND = 0.454;final double METERS_PER_FOOT = 0.3046;
System.out.println("BMI Calculator\n");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter weight (lbs): ");double weight = Double.parseDouble(stdin.readLine());
System.out.print("Enter height (feet): ");double height = Double.parseDouble(stdin.readLine());
double metricWeight = weight * KILOGRAMS_PER_POUND;double metricHeight = height * METERS_PER_FOOT;
double bmi = metricWeight / (metricHeight * metricHeight);
System.out.println("A person with");System.out.println(" weight " + weight + " (lbs)");System.out.println(" height " + height + " (feet)");System.out.println("has a BMI of " + bmi);
}
![Page 48: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/48.jpg)
Accessing the standard input stream Extraction
System.out.print("Enter weight (lbs): ");double weight = Double.parseDouble(stdin.readLine());
System.out.print("Enter height (feet): ");double height = Double.parseDouble(stdin.readLine());
![Page 49: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/49.jpg)
Primitive variable assignment Assignment operator =
Allows the memory location for a variable to be updated
Considerint j = 11;j = 1985;
11j
Expression to beevaluated
Name of previouslydefined object
target = expression ;
![Page 50: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/50.jpg)
Assignment operator = Allows the memory location for a variable to be updated
Considerint j = 11;j = 1985;
1985j
Primitive variable assignment
Expression to beevaluated
Name of previouslydefined object
target = expression ;
![Page 51: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/51.jpg)
Primitive variable assignment Consider
int a = 1;int aSquared = a * a;a = 5;aSquared = a * a;
Considerint i = 0;i = i + 1;
Considerint asaRating;asaRating = 400;
1a
1aSquared
![Page 52: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/52.jpg)
Primitive variable assignment Consider
int a = 1;int aSquared = a * a;a = 5;aSquared = a * a;
Considerint i = 0;i = i + 1;
Considerint asaRating;asaRating = 400;
5a
1aSquared
![Page 53: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/53.jpg)
Primitive variable assignment Consider
int a = 1;int aSquared = a * a;a = 5;aSquared = a * a;
Considerint i = 0;i = i + 1;
Considerint asaRating;asaRating = 400;
5a
25aSquared
![Page 54: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/54.jpg)
Primitive variable assignment Consider
int a = 1;int aSquared = a * a;a = 5;aSquared = a * a;
Considerint i = 0;i = i + 1;
Considerint asaRating;asaRating = 400;
0i
![Page 55: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/55.jpg)
Primitive variable assignment Consider
int a = 1;int aSquared = a * a;a = 5;aSquared = a * a;
Considerint i = 0;i = i + 1;
Considerint asaRating;asaRating = 400;
1i
![Page 56: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/56.jpg)
Primitive variable assignment Consider
int a = 1;int aSquared = a * a;a = 5;aSquared = a * a;
Considerint i = 0;i = i + 1;
Considerint asaRating;asaRating = 400;
-asaRating
![Page 57: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/57.jpg)
Primitive variable assignment Consider
int a = 1;int aSquared = a * a;a = 5;aSquared = a * a;
Considerint i = 0;i = i + 1;
Considerint asaRating;asaRating = 400;
400asaRating
![Page 58: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/58.jpg)
Primitive variable assignment Consider
double x = 5.12;double y = 19.28;double rememberX = x; x = y; y = rememberX;
5.12x
![Page 59: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/59.jpg)
Primitive variable assignment Consider
double x = 5.12;double y = 19.28;double rememberX = x; x = y; y = rememberX;
5.12x
19.28y
![Page 60: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/60.jpg)
Primitive variable assignment Consider
double x = 5.12;double y = 19.28;double rememberX = x; x = y; y = rememberX;
5.12x
19.28y
5.12rememberX
![Page 61: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/61.jpg)
Primitive variable assignment Consider
double x = 5.12;double y = 19.28;double rememberX = x; x = y; y = rememberX;
19.28x
19.28y
5.12rememberX
![Page 62: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/62.jpg)
Primitive variable assignment Consider
double x = 5.12;double y = 19.28;double rememberX = x; x = y; y = rememberX;
19.28x
5.12y
5.12rememberX
![Page 63: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/63.jpg)
Increment and decrement operators ++
Increments a number variable by 1 --
Decrements a numeric variable by 1
Considerint i = 4;++i;System.out.println(i);System.out.print(++i);System.out.println(i++);System.out.println(i);
![Page 64: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/64.jpg)
Increment and decrement operators ++
Increments a number variable by 1 --
Decrements a numeric variable by 1
Considerint i = 4; // define++i;System.out.println(i);System.out.print(++i);System.out.println(i++);System.out.println(i);
4i
![Page 65: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/65.jpg)
Increment and decrement operators ++
Increments a number variable by 1 --
Decrements a numeric variable by 1
Considerint i = 4;++i; // incrementSystem.out.println(i);System.out.print(++i);System.out.println(i++);System.out.println(i);
5i
![Page 66: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/66.jpg)
Increment and decrement operators ++
Increments a number variable by 1 --
Decrements a numeric variable by 1
Considerint i = 4;++i;System.out.println(i); // displaySystem.out.print(++i);System.out.println(i++);System.out.println(i);
5i
![Page 67: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/67.jpg)
Increment and decrement operators ++
Increments a number variable by 1 --
Decrements a numeric variable by 1
Considerint i = 4;++i;System.out.println(i);System.out.print(++i); // update then displaySystem.out.println(i++); System.out.println(i);
6i
![Page 68: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/68.jpg)
Increment and decrement operators ++
Increments a number variable by 1 --
Decrements a numeric variable by 1
Considerint i = 4;++i;System.out.println(i);System.out.print(++i); System.out.println(i++); // display then update System.out.println(i);
7i
![Page 69: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/69.jpg)
Increment and decrement operators ++
Increments a number variable by 1 --
Decrements a numeric variable by 1
Considerint i = 4;++i;System.out.println(i);System.out.print(++i);System.out.println(i++);System.out.println(i); // display
7i
![Page 70: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/70.jpg)
Escape sequences Java provides escape sequences for printing special
characters \b backspace \n newline \t tab \r carriage return \\ backslash \" double quote \' single quote
![Page 71: Java basics. Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943](https://reader035.vdocument.in/reader035/viewer/2022062305/5697c0201a28abf838cd2338/html5/thumbnails/71.jpg)
Escape sequences What do these statements output?
System.out.println("Person\tHeight\tShoe size");System.out.println("=========================");System.out.println("Hannah\t5‘1\"\t7");System.out.println("Jenna\t5'10\"\t9");System.out.println("JJ\t6'1\"\t14");
Output
Person Height Shoe size=========================Hannah 5‘1" 7Jenna 5'10" 9JJ 6'1" 14