17
Introduction to Computing Concepts Note Set 11

Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

Embed Size (px)

Citation preview

Page 1: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

Introduction to Computing ConceptsNote Set 11

Page 2: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

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”

Page 3: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

On Windows

http://blogs.oreilly.com/digitalmedia/uploads/2008/04/unix4mac-terminal.png

Windows Command Prompt

Mac OS X Terminal

Page 4: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

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 WorldWhat is your name?Hello123

Page 5: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

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

Page 6: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

System.out.println(…) – some notesSystem.out.println(“Hello “ + “world.”);

System.out.println(“Grade:” + 97);

Page 7: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

On Slides:

To Save Space on Slides:

print (…);

means

System.out.println(…);

Page 8: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

Back to ifs… What’s the difference between…

this…

if ( grade >= 90 ) print(“A”);if ( grade >= 80 ) print (“B”);if (grade >= 70 ) print (“C”);

and this…

if ( grade >= 90 ) print(“A”);else if ( grade >= 80 ) print (“B”);else if (grade >= 70 ) print (“C”);

Page 9: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

Nesting Control Statements•Nested – ifs▫having one if statement inside another

if (a > b) {

if (b < c) {

print(“cool”);

}

print (“awesome”);

}

Will be executed if b < c AND a > b

Will be executed if a > bregardless of truth of b < c

Page 10: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

Nesting Control Statementsif (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: ______________________________

Page 11: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

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));

Page 12: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

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

Page 13: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

Wage Calculator: Example

Application RequirementsA 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.

Page 14: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

private void calcualteJButtonActionPerformed(ActionEvent event){ //Get Data from text fields

Page 15: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

//Declare constant and determine over time using ifs

Page 16: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

//Format and display data

Page 17: Introduction to Computing Concepts Note Set 11. Other ways of interacting with Java programs There are: ▫ GUI programs ▫ Web-based applets (GUI programs

Breakout 1 & 2