System.out.println()
Download
Report
Transcript System.out.println()
// Java0501.java
// This program (and the next few programs) demonstrate user keyboard input
// during program execution. This particular program demonstrates keyboard
// input of a string in a text window using the <Expo> class.
public class Java0501
{
public static void main (String args[])
{
System.out.println();
System.out.println("JAVA0501.JAVA");
System.out.println();
System.out.print("Enter name ===>> ");
String name = Expo.enterString();
System.out.println();
System.out.println("Name Entered:
System.out.println();
// Line 1
// Line 2
" + name);
}
}
Line 1 is called the prompt. It asks or “prompts” the user for information.
Line 2 is the line that actually enters the String and stores it in name.
// Java0502.java
// This program demonstrates how to use Expo.enterString() for
// three separate String keyboard inputs.
public class Java0502
{
public static void main (String args[])
{
System.out.println("\nJAVA0502.JAVA\n");
System.out.print("Enter Line 1 ===>> ");
String input1 = Expo.enterString();
System.out.print("Enter Line 2 ===>> ");
String input2 = Expo.enterString();
System.out.print("Enter Line 3 ===>> ");
String input3 = Expo.enterString();
System.out.println();
System.out.println(input1);
System.out.println(input2);
System.out.println(input3);
System.out.println();
}
}
// Java0503.java
// This program demonstrates <String> objects concatenation with
// keyboard entered data.
public class Java0503
{
public static void main (String args[])
{
System.out.println("\nJAVA0503.JAVA\n");
System.out.print("Enter 1st Number ===>> ");
String number1 = Expo.enterString();
System.out.print("Enter 2nd Number ===>> ");
String number2 = Expo.enterString();
String sum = number1 + number2;
System.out.println();
System.out.println(number1 + " + " + number2 + " = " + sum);
System.out.println();
}
}
Addition vs. Concatenation
The problem with the previous program is that
Strings were used instead of ints or doubles.
When the plus sign ( + ) is used with a numerical
value, like an int or a double, it performs addition.
However, when the plus sign is used with Strings,
it joins the Strings together.
This is called String Concatenation.
// Java0504.java
// This program uses the Expo.enterInt() method to enter integers from
// the keyboard. It is now possible to correctly add the two numbers.
public class Java0504
{
public static void main (String args[])
{
System.out.println("\nJAVA0504.JAVA\n");
System.out.print("Enter 1st Number ===>> ");
int number1 = Expo.enterInt();
System.out.print("Enter 2nd Number ===>> ");
int number2 = Expo.enterInt();
int sum = number1 + number2;
System.out.println();
System.out.println(number1 + " + " + number2 + " = " + sum);
System.out.println();
}
}
// Java0505.java
// This program demonstrates how to use Expo.enterDouble() for three
// separate double keyboard inputs, which are used to display the mean.
public class Java0505
{
public static void main (String args[])
{
System.out.println("\nJAVA0505.JAVA\n");
System.out.print("Enter Number 1 ===>> ");
double n1 = Expo.enterDouble();
System.out.print("Enter Number 2 ===>> ");
double n2 = Expo.enterDouble();
System.out.print("Enter Number 3 ===>> ");
double n3 = Expo.enterDouble();
System.out.println();
System.out.println(n1);
System.out.println(n2);
System.out.println(n3);
double mean = (n1 + n2 + n3) / 3;
System.out.println();
System.out.println("The mean is " + mean);
System.out.println();
}
}
// Java0506.java
// This program demonstrates how to use Expo.enterChar() which is ideal
// for entering a single letter.
public class Java0506
{
public static void main (String args[])
{
System.out.println("\nJAVA0506.JAVA\n");
System.out.print("Enter First Name: ===>> ");
String firstName = Expo.enterString();
System.out.print("Enter Middle Initial: ===>> ");
char middleInitial = Expo.enterChar();
System.out.print("Enter Last Name: ===>> ");
String lastName = Expo.enterString();
System.out.println();
System.out.println("Your full name is: " + firstName + " " +
middleInitial + ". " + lastName);
System.out.println();
}
}
Expo class Input Methods
Expo.enterInt() is used to enter an int from the text screen.
Expo.enterDouble() is used to enter a double from the text screen.
Expo.enterString() is used to enter a String from the text screen.
Expo.enterChar() is used to enter a char from the text screen.
Program Flow
Program Flow follows the exact
sequence of listed program statements,
unless directed otherwise by a Java
control structure.
Types of
Control Structures
•
Simple Sequence
•
Selection also called:
-
•
Decision Making
Conditional Branching
Alternation
Repetition also called:
-
Looping
Iteration
Simple Sequence
Program Statement
Program Statement
Program Statement
Program Statement
One-Way Selection
Program Statement
Condition
True
False
Program Statement
Program Statement
Program Statement
Two-Way Selection
Program Statement
True
Condition
False
Program Statement
Program Statement
Program Statement
Program Statement
Multiple-Way Selection
Program Statement
Condition
False
Condition
False
Condition
True
True
True
False
Program Statement
Program Statement
Program Statement
Program Statement
Repetition
Program Statement
Program Statement
Program Statement
Condition
True
False
Program Statement
Conditional Statement
Definition
A conditional statement is a
program expression that
evaluates to true or false.
Most conditional statements
require a relational operator.
All conditions must be placed
inside (parentheses).
Relational Operators
Name
Operator Expression Evaluates
Less than or
equals
==
!=
<
>
<=
Greater than
or equals
>=
Equals
Not Equals
Less than
Greater than
5 == 5
5 == 10
true
false
50 != 25
100 != 100
true
false
100 < 200
200 < 100
200 > 100
200 > 200
100 <= 200
200 <= 200
200 <= 100
100 >= 200
200 >= 200
200 >= 100
true
false
true
false
true
true
false
false
true
true
Important Note:
The relational operators shown on the previous
slide will be used in the Java example programs
that demonstrate the different control structures.
Be careful not to confuse the equality operator
( == ) with the assignment operator ( = ).
// Java0507.java
// This program demonstrates one-way selection with <if>.
// Run the program twice. First with Sales equals to 300,000
// and a second time with Sales equals 500,000.
public class Java0507
{
public static void main (String args[])
{
System.out.println("\nJAVA0507.JAVA\n");
System.out.print("Enter Sales ===>> ");
double sales = Expo.enterDouble();
double bonus = 0.0;
System.out.println();
if (sales >= 500000.0)
bonus = 1000.0;
System.out.println(“Christmas bonus:
System.out.println();
}
}
" + bonus);
Indentation Rule:
Java syntax uses freeform program style.
Program statements may be placed on multiple
lines with or without indentation.
By convention, control structures and their
conditional statements are placed on one line.
The program statement that is executed, if the
condition is true, is placed on the next line, and
indented below the conditional statement.
if(sales >= 500000)
bonus = 1000;
if(sales >=500000) bonus = 1000;
// Java0508.java
// This program demonstrates one-way selection with <if>.
// It also shows that only one statement is controlled. Run the program twice.
// First with Sales equals to 300,000 and then a 2nd time with Sales equals to 500,000.
public class Java0508
{
public static void main (String args[])
{
System.out.println("\nJAVA0508.JAVA\n");
System.out.print("Enter Sales ===>> ");
double sales = Expo.enterDouble();
double bonus = 0.0;
System.out.println();
if (sales >= 500000.0)
System.out.println("CONGRATULATIONS!");
System.out.println("You sold half a million dollars in merchandise!");
System.out.println("You will receive a $1000 Christmas Bonus!");
System.out.println("Keep up the good work!");
bonus = 1000.0;
System.out.println();
System.out.println("Christmas bonus:
System.out.println();
}
}
" + bonus);
// Java0509.java
// This program demonstrates one-way selection with <if>.
// It fixes the logic problem of the previous program with block structure by using braces.
public class Java0509
{
public static void main (String args[])
{
System.out.println("\nJAVA0509.JAVA\n");
System.out.print("Enter Sales ===>> ");
double sales = Expo.enterDouble();
double bonus = 0.0;
System.out.println();
if (sales >= 500000.0)
{
System.out.println("CONGRATULATIONS!");
System.out.println("You sold half a million dollars in merchandise!");
System.out.println("You will receive a $1000 Christmas Bonus!");
System.out.println("Keep up the good work!");
bonus = 1000.0;
}
System.out.println();
System.out.println("Christmas bonus:
System.out.println();
}
}
" + bonus);
One-Way Selection Syntax
One-Way selection general syntax:
if (condition true)
execute program statement
if (counter > 100)
System.out.println("Counter exceeds 100");
Use braces { } and block structure to control multiple
program statements.
if (savings >= 10000)
{
System.out.println("It’s skiing time");
System.out.println("Let’s pack");
System.out.println("Remember your skis");
}
// Java0510.java
// This program demonstrates two-way selection with <if..else>.
// Run the program twice: First with 1200, then with 1000.
public class Java0510
{
public static void main (String args[])
{
System.out.println("\nJAVA0510.JAVA\n");
System.out.print("Enter SAT ===>> ");
int sat = Expo.enterInt();
System.out.println();
if (sat >= 1100)
System.out.println("You are admitted");
else
System.out.println("You are not admitted");
System.out.println();
}
}
// Java0511.java
// This program demonstrates two-way selection with <if..else>.
// Multiple statements require the use of block structure.
// Run the program twice: First with 1100, then with 1099.
public class Java0511
{
public static void main (String args[])
{
System.out.println("\nJAVA0511.JAVA\n");
System.out.print("Enter SAT ===>> ");
int sat = Expo.enterInt();
System.out.println();
if (sat >= 1100)
{
System.out.println("You are admitted");
System.out.println("Orientation will start in June");
}
else
{
System.out.println("You are not admitted");
System.out.println("Please try again when your SAT improves.");
}
System.out.println();
}
}
Two-Way Selection Syntax
Two-Way selection general syntax:
if (condition true)
execute first program statement
else // when condition is false
execute second program statement
if (gpa >= 90.0)
System.out.println ( "You’re an honor graduate");
else
System.out.println ("You’re not an honor graduate");
// Java0512.java
// This program demonstrates multi-way selection with <switch> and <case>.
// This program compiles, but displays illogical output.
public class Java0512
{
public static void main (String args[])
{
System.out.println("\nJAVA0512.JAVA\n");
System.out.print("Enter Letter Grade ===>> ");
char grade = Expo.enterChar();
System.out.println();
switch (grade)
{
case 'A' : System.out.println("90 .. 100 Average");
case 'B' : System.out.println("80 .. 89 Average");
case 'C' : System.out.println("70 .. 79 Average");
case 'D' : System.out.println("60 .. 69 Average");
case 'F' : System.out.println("Below 60 Average");
}
System.out.println();
}
}
// Java0513.java
// This program demonstrates multi-way selection with <switch> and <case>.
// The program adds <break> and <default>. The use of <break> is required
// for logical output. The <default> case occurs when no other case matches.
public class Java0513
{
public static void main (String args[])
{
System.out.println("\nJAVA0513.JAVA\n");
System.out.print("Enter Letter Grade ===>> ");
char grade = Expo.enterChar();
System.out.println();
switch (grade)
{
case 'A'
: System.out.println("90 .. 100 Average");
case 'B'
: System.out.println("80 .. 89 Average");
case 'C'
: System.out.println("70 .. 79 Average");
case 'D'
: System.out.println("60 .. 69 Average");
case 'F'
: System.out.println("Below 60 Average");
default
: System.out.println("No Match Found");
}
System.out.println();
}
}
break;
break;
break;
break;
break;
Program Note
In order to focus on the important and
relevant parts of each program, several
programs will not be shown in their
entirety. Rather, a segment of the
program will be shown that focuses on
the key point. You have the complete
programs on your computer.
// Java0514.java
// This program demonstrates that multiple program statements
// can be placed between the <case> and the <break> statements.
System.out.print("Enter Letter Grade ===>> ");
char grade = Expo.enterChar();
switch (grade)
{
case 'A' :
System.out.println("90 .. 100 Average");
System.out.println("Excellent!");
break;
case 'B' :
System.out.println("80 .. 89 Average");
System.out.println("Good");
break;
case 'C' :
System.out.println("70 .. 79 Average");
System.out.println("Fair");
break;
case 'D' :
System.out.println("60 .. 69 Average");
System.out.println("Poor");
break;
case 'F' :
System.out.println("Below 60 Average");
System.out.println("Bad");
break;
default :
System.out.println("No Match Found");
}
// Java0515.java
// This program demonstrates how to allow both capital and lowercase letters,
switch (grade)
{
case 'A' :
case 'a' :
System.out.println("90 .. 100 Average");
System.out.println("Excellent!");
break;
case 'B' :
case 'b' :
System.out.println("80 .. 89 Average");
System.out.println("Good");
break;
case 'C' :
case 'c' :
System.out.println("70 .. 79 Average");
System.out.println("Fair");
break;
case 'D' :
case 'd' :
System.out.println("60 .. 69 Average");
System.out.println("Poor");
break;
case 'F' :
case 'f' :
System.out.println("Below 60 Average");
System.out.println("Bad");
break;
default :
System.out.println("No Match Found");
}
// Java0516.java
// This program demonstrates multi-way selection can also be controlled
// with an <int> variable. Both <int> and <char> work with <switch>.
// Both <String> and <double> do not because they are not "ordinal".
System.out.print("What grade are you in? ===>> ");
int grade = Expo.enterInt();
switch (grade)
{
case 0 : case 1 : case 2 : case 3 : case 4 : case 5 :
System.out.println("Elementary School");
break;
case 6 : case 7 : case 8 :
System.out.println("Middle School");
break;
case 9 : case 10 : case 11 : case 12 :
System.out.println("High School");
break;
case 13 : case 14 : case 15 : case 16 :
System.out.println("College");
break;
default :
System.out.println("Graduate School");
}
Multiple-Way Selection Syntax
Multiple-Way selection General Syntax
switch(selectionVariable)
{
case selectionConstant : program statement(s); break;
case selectionConstant : program statement(s); break;
default
: program statement(s);
}
Specific Example:
switch(courseGrade)
{
case ’A’ : points = 4; break;
case ’B’ : points = 3; break;
case ’C’ : points = 2; break;
case ’D’ : points = 1; break;
case ’F’ : points = 0; break;
default
: System.out.println(“Error”);
}
// Java0517.java
// This program displays 40 identical lines very inefficiently
// with 40 separate println statements.
public class Java0517
{
public static void main(String args[])
{
System.out.println("\nJAVA0517.JAVA\n");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
System.out.println("Eat at Joe's friendly diner for the best lunch value");
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
// Java0518.java
// This program displays 40 identical lines efficiently
// with one println statement and a loop structure.
public class Java0518
{
public static void main(String args[])
{
System.out.println("\nJAVA0518.JAVA\n");
int k;
for (k = 1; k <= 40; k++)
System.out.println("Eat at Joe's friendly diner for the best lunch value");
}
}
// Java0518.java
// This program displays 40 identical lines efficiently
// with one println statement and a loop structure.
public class Java0518
{
public static void main(String args[])
{
System.out.println("\nJAVA0518.JAVA\n");
int k;
for (k = 1; k <= 40; k++)
System.out.println("Eat at Joe's friendly diner for the best lunch value");
}
} Part 1 is used to
initialize the counter
(Loop Control
Variable).
// Java0518.java
// This program displays 40 identical lines efficiently
// with one println statement and a loop structure.
public class Java0518
{
public static void main(String args[])
{
System.out.println("\nJAVA0518.JAVA\n");
int k;
for (k = 1; k <= 40; k++)
System.out.println("Eat at Joe's friendly diner for the best lunch value");
}
} Part 1 is used to
initialize the counter
(Loop Control
Variable).
Part 3 indicates
what the counter
counts by. ++
means count by 1.
// Java0518.java
// This program displays 40 identical lines efficiently
// with one println statement and a loop structure.
public class Java0518
{
public static void main(String args[])
{
System.out.println("\nJAVA0518.JAVA\n");
int k;
for (k = 1; k <= 40; k++)
System.out.println("Eat at Joe's friendly diner for the best lunch value");
}
} Part 1 is used to
initialize the counter
(Loop Control
Variable).
Part 2 is a condition.
As long as it is true
the loop will keep
repeating.
Part 3 indicates
what the counter
counts by. ++
means count by 1.
// Java0519.java
// This program displays consecutive numbers 1 through 15.
// It also shows how the loop control variable may be
// defined inside the <for> program statement.
public class Java0519
{
public static void main(String args[])
{
System.out.println("\nJAVA0519.JAVA\n");
for (int k = 1; k <= 15; k++)
System.out.print(k + " ");
System.out.println();
}
}
// Java0520.java
// This program demonstrates how to use block structure
// with a <for> loop control structure.
public class Java0520
{
public static void main(String args[])
{
System.out.println("\nJAVA0520.JAVA\n");
int k;
for (k = 1; k <= 10; k++)
{
System.out.println("####################################");
System.out.println("Line Number " + k);
}
System.out.println();
}
}
// Java0521.java
// This program displays various counting schemes.
// It also demonstrates the versatility of the <for> loop.
public class Java0521
{
public static void main(String args[])
{
System.out.println("\nJAVA0521.JAVA\n");
for (int p = 1; p <= 15; p++)
System.out.print(p + " ");
You do NOT always have to use
System.out.println();
++ in the 3rd part of a for loop.
for (int q = 1; q <= 15; q+=3)
System.out.print(q + " ");
You can count by any amount.
System.out.println();
for (int r = 15; r >= 1; r--)
System.out.print(r + " ");
You can count backwards.
System.out.println();
for (double s = 0; s <= 3; s+=0.5)
System.out.print(s + " ");
You can count by fractional
System.out.println();
amounts.
for (char t = 'A'; t <= 'Z'; t++)
System.out.print(t + " ");
You can even count with
System.out.println("\n\n");
}
characters.
}
Repetion Control
Structures With for
Java has a variety of control structures for repetition.
Other computer science terms for repetition are looping and iteration.
Fixed Iteration is done with the for loop structure.
for loop syntax:
for (Part1; Part2; Part3)
loop body;
The for loop has three distinct parts:
Part1 initializes the Loop Control Variable (LCV).
Part2 sets the exit condition for the loop.
Part3 determines how the LCV changes.
for (k = 1; k <= 10; k++)
System.out.println("Java is 10 times more fun");
// Java0522.java
// This program is supposed to keep repeating until a correct PIN# of 5678 is entered.
// The program does not work because the <for> loop is used in an inappropriate manner.
// The <for> loop is meant for "fixed" repetition.
// Entering a PIN# is an example of "conditional" repetition.
public class Java0522
{
public static void main(String args[])
{
System.out.println("\nJAVA0522.JAVA\n");
for (int j = 1; j <= 10; j++)
{
System.out.println();
System.out.print("Enter 4 digit PIN#. --> ");
int pin = Expo.enterInt();
if (pin != 5678)
System.out.println("Incorrect Password. Try Again.");
}
System.out.println("\nYou are now logged in. Welcome to the program.");
}
}
// Java0523.java
// This program fixes the problem of the previous program by using a while command.
// Now the loop will stop when the correct PIN# of 5678 is entered.
public class Java0523
{
public static void main(String args[])
{
System.out.println("\nJAVA0523.JAVA\n");
int pin = 0;
while (pin != 5678)
{
System.out.println();
System.out.print("Enter 4 digit PIN#. --> ");
pin = Expo.enterInt();
if (pin != 5678)
System.out.println("Incorrect Password. Try Again.");
}
System.out.println("\nYou are now logged in. Welcome to the program.");
}
}
Flags
The flag is the special value that makes the loop stop.
Maybe this value is entered by the user.
Maybe it is computed in the loop.
Whatever special value makes the loop stop is the flag.
A good way to remember that is
to think of a race track.
Cars go in a loop again and again.
The race keeps going until the
checkered flag is waved and the
race is over.
Repetion Control
Structures With while
while loop syntax:
initialize condition variable
while(condition is true)
loop body
alter condition variable in loop body
x = 0;
// initialize condition variable
while(x < 10)
{
x++;
// alter condition variable
System.out.println("x = " + x);
}
Program Segment
NoNo #1
int x = 0;
while(x < 10)
System.out.println(x);
Program Segment
YesYes #1
int x = 0;
while(x < 10)
{
x++;
System.out.println(x);
}
The loop condition
The loop condition variable,
variable, x, never changes. x, changes. The loop exits
when x reaches 10.
The loop will not exit.
Program Segment
NoNo #2
Program Segment
YesYes #2
int x;
while(x < 10)
{
x++;
System.out.println(x);
}
int x = 0;
while(x < 10)
{
x++;
System.out.println(x);
}
The loop condition
variable, x, is never
initialized. This program
will not compile in Java.
The loop condition
variable, x, is initialized.
The program will compile
and execute normally.
Fixed Repetition vs.
Conditional Repetition
Fixed Repetition describes a situation where you know – ahead of
time – how many times you want the loop to repeat.
An example would be drawing exactly 100 circles on then screen.
The command for fixed repetition is for.
Conditional Repetition describes a situation where you do NOT
know how many times the loop will repeat.
The loop has to repeat until some condition is met.
An example would be entering a password.
The command for conditional repetition is while.
// Java0524.java
// This program shows how a control structure can be used with graphics.
// This program draws vertical lines, because the starting x and
// ending x values are the same.
import java.awt.*;
import java.applet.*;
public class Java0524 extends Applet
{
public void paint(Graphics g)
{
int x1 = 100;
int y1 = 100;
int x2 = 100;
int y2 = 500;
for (int k = 1; k <= 81; k++)
{
Expo.drawLine(g,x1,y1,x2,y2);
x1 += 10;
x2 += 10;
}
}
}
// Java0525.java
// This program shows how a control structure can be used with graphics.
// This program draws horizontal lines, because the starting y and
// ending y values are the same.
import java.awt.*;
import java.applet.*;
public class Java0525 extends Applet
{
public void paint(Graphics g)
{
int x1 = 100;
int y1 = 50;
int x2 = 900;
int y2 = 50;
for (int k = 1; k <= 50; k++)
{
Expo.drawLine(g,x1,y1,x2,y2);
y1 += 10;
y2 += 10;
}
}
}
// Java0526.java
// This program shows how a control structure can be used with graphics.
// This program draws diagonal lines, because all 4 variables -- x1, y1, y2, y2 -- change.
import java.awt.*;
import java.applet.*;
public class Java0526 extends Applet
{
public void paint(Graphics g)
{
int x1 = 50;
int y1 = 50;
int x2 = 200;
int y2 = 300;
for (int k = 1; k <= 60; k++)
{
Expo.drawLine(g,x1,y1,x2,y2);
x1 += 10;
x2 += 10;
y1 += 5;
y2 += 5;
}
}
}
// Java0527.java
// This program demonstrates several lines with the same starting point.
// In this case the (x1,y1) coordinate stays fixed and the (x2,y2) point changes.
import java.awt.*;
import java.applet.*;
public class Java0527 extends Applet
{
public void paint(Graphics g)
{
int x1 = 50;
int y1 = 50;
int x2 = 900;
int y2 = 50;
for (int k = 1; k <= 50; k++)
{
Expo.drawLine(g,x1,y1,x2,y2);
y2 += 10;
x2 -= 15;
}
}
}
// Java0528.java
// This program demonstrates several ovals.
// All of the ovals have the same center and vertical radius.
// The horizontal radius keeps changing.
import java.awt.*;
import java.applet.*;
public class Java0528 extends Applet
{
public void paint(Graphics g)
{
int x = 500;
int y = 325;
int hr = 50;
int vr = 100;
for (int k = 1; k <= 40; k++)
{
Expo.drawOval(g,x,y,hr,vr);
hr += 10;
}
}
}