Note Set 11

advertisement
Introduction to Computing
Concepts
Note Set 11
Other ways of interacting with Java programs
• There are:
▫ GUI programs
▫ Web-based applets (GUI programs that run in a
browser)
▫ Command line interface programs
 Windows Based: Run in command prompt/Dos prompt
 From a RUN dialog, type ‘cmd’ and hit enter
 Macs or Linux: Run in a Terminal Window
 Mac: Look in Utilities in applications
 Linux: depends on distro – look under accessories on “main
menu”
On Windows
Mac OS X
Terminal
Windows Command Prompt
http://blogs.oreilly.com/digitalmedia/uploads/2008/04/unix
4mac-terminal.png
System.out.println(…)
•
•
•
•
•
Used in CLI program
Can be used in gui program for debugging purposes
Accepts a String argument and will print it
Prints the argument then goes to the next line (new line)
Examples:
System.out.println (“Hello World”);
System.out.println(“What” + “ is “ + “ your name?”);
System.out.println(“Hello” + 123);
Hello World
What is your name?
Hello123
System.out.println()
• We’ll use this to do some quick testing in Java
• Can be used to write fully-operational complex pieces of
software
• In Netbeans, output will appear in the Terminal Output
Window at the bottom
System.out.println(…) – some notes
System.out.println(“Hello “ + “world.”);
System.out.println(“Grade:” + 97);
On Slides:
To Save Space on Slides:
print (…);
means
System.out.println(…);
Back to ifs…
What’s the difference between…
this…
and this…
if ( grade >= 90 )
print(“A”);
if ( grade >= 80 )
print (“B”);
if (grade >= 70 )
print (“C”);
if ( grade >= 90 )
print(“A”);
else if ( grade >= 80 )
print (“B”);
else if (grade >= 70 )
print (“C”);
Nesting Control Statements
• Nested – ifs
▫ having one if statement inside another
if (a > b) {
if (b < c) {
Will be executed if
b < c AND a > b
print(“cool”);
}
print (“awesome”);
}
Will be executed if a > b
regardless of truth of b < c
Nesting Control Statements
if (a > b) {
if (b < c) {
print(“cool”);
}
print (“awesome”);
}
Assume:
a = 3, b = 2, c = 25. Output: _____________________________
a = 4, b = 6, c = 8.
Output: ______________________________
a = 4, b = -2, c = -9. Output: ______________________________
Decimal Format
• Some contexts have floating point values but only
with a certain amt of precision
▫ MONEY!!!!!!
• Use a DecimalFormat object to help format how FP
data is displayed
double wages = 3.44567651;
DecimalFormat dollars = new DecimalFormat(“$0.00”);
myJLabel.setText(dollars.format(wages));
Constants
• Store a constant value.
• Uses keyword final to declare
final int PI = 3.1415;
• If an attempt is made to change the value, compiler
will indicate an error
• Makes reading source code easier. If constant
changes, only have to change in one place.
▫ Think sales tax rate or commission rate for a job.
▫ Could use many places but when changes, only change
in one place
Wage Calculator: Example
Application Requirements
A company needs an application that calculates the gross wages per week for
each of its employees. Each employee’s weekly salary is based on the
employee’s number of hours worked and hourly wage. A standard work week is
40 hours. Any time worked over 40 hours in a week is considered “overtime,”
and employees earn time-and-a-half for the extra hours. Create an application
that accepts one employee’s number of hours worked and hourly wage, and
calculates the employee’s total (gross) wages for the week.
private void calcualteJButtonActionPerformed(ActionEvent event)
{
//Get Data from text fields
//Declare constant and determine over time using ifs
//Format and display data
Download