Learning Java Part 2 - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Learning Java Part 2

Description:

New egg. Brick-and-motor stores online. Barnes and Nobles. Online store receipt. Receipt of items ... 3 lines in green must match. Totals must be to 2 decimal places ... – PowerPoint PPT presentation

Number of Views:105
Avg rating:3.0/5.0
Slides: 44
Provided by: scie2
Category:
Tags: java | learning | newegg | part

less

Transcript and Presenter's Notes

Title: Learning Java Part 2


1
Learning Java Part 2
  • Dorian Miller

2
  • Hooray it is
  • Much needed for drought in NC
  • Raining

3
Announcements
  • Posted on BlackBoard
  • Solution to midterm and programs
  • Slides on scores/grades
  • Java/ Eclipse
  • Complete installation by Fridays recitation
  • Assistance office hours, email, ITS help
  • Program 4 Online store
  • JavaScript
  • Due Thu Nov 1, 1159pm EST

4
Big picture
  • Learning a new programming language, Java
  • Questions to ask
  • Purpose of language
  • Variables
  • Input/output
  • Control structures
  • Functions

5
Program 4 Online storefront(JavaScript)
  • Ecommerce
  • Electronic business on the web
  • Part of new economy
  • Online stores
  • Amazon
  • Ebay
  • New egg
  • Brick-and-motor stores online
  • Barnes and Nobles

6
Online store receipt
  • Receipt of items
  • Customer selects shopping items
  • Click item buttons
  • Customer checks out, gets total
  • Click total button
  • Reset shopping list
  • Click clear button

7
Program 4 tester
  • Previous version
  • Text must match exactly
  • This version
  • 3 lines in green must match. Totals must be to 2
    decimal places
  • Total number of lines must match, in example 8
    lines
  • Lines in blue will not match, everyone will have
    unique items
  • -----------
  • Subtotal 92.76
  • Tax 6.49
  • Total 99.25
  • item 4 50.00
  • item 3 31.22
  • item 2 10.55
  • item 1 0.99

8
Design requirements
  • Design the web page interface
  • Store name and description
  • List items and price
  • Buttons of shopping items must use the same
    function
  • Must declare constant arrays (you pick names)
  • var PRICE 0.99, 10.55, 31.22, 50.00
  • var NAMES "Item 1", "Item 2", "Item 3", "Item
    4"

9
Text Area HTML form field
  • 2 HTML tag
  • Multiple lines of text
  • Initial text goes between tags
  • Rows and cols sets size of text area

10
Text area text
  • Text in text area is a string
  • Text in receipt is on multiple lines
  • Subtotal 92.76
  • Tax 6.49
  • Total 99.25
  • Use special character to create new line
  • \n (slash n)
  • Example of string
  • myString Subtotal 92.76\nTax 6.49\nTotal
    99.25\n
  • or
  • myString Subtotal 92.76 \n
  • myString myString Tax 6.49\n Total
    99.25\n

11
Escape characters
  • Special characters called escape characters
  • \ (slash) indicates start of escape char
  • Examples
  • \n new line
  • \t tab, a larger space
  • \\ the \ (slash) character
  • \ the quote character

12
Input and Output
  • Graphical User Interface (GUI)
  • Modern interface
  • Consists of text fields, buttons, slides, etc
  • Input
  • Enter text
  • Click and drag with mouse
  • Output
  • Graphical display changes
  • Dialog messages

13
Text based interface
  • Text based interface
  • Interface before GUI
  • Monitor and keyboard referred to as console
  • Example Computer text, User text
  • Please enter your login
  • Comp110 Console
  • Please enter your password
  • Enter student name
  • Jane Doe

14
Why learn text based interface
  • Java
  • Text based is simplest input/output
  • Java capable of GUI
  • Experience how computer manages input
  • Low level understanding of keyboard
  • GUI is based on low level input
  • Geeky knowledge to share at cocktail party

15
Output
  • Output
  • Display text on monitor
  • Java text output command
  • System.out.println(String to display")

16
Keyboard Input
  • User enters input
  • Text
  • Other characters (enter key)
  • User input
  • I typed each character (enter)
  • In the last two lines
  • Keystrokes stored in buffer
  • Array of characters
  • I typed each character \n In the last two lines

17
Keyboard input in Java program
  • Scanner object
  • Reads data from the keyboard
  • Convention object name starts with capital
    letter. Distinguish from variables and constants
  • Step 1 Learn about Scanner documentation
  • Search web Java Scanner
  • Get description
  • Get data and functions

18
Step 2 Declare Scanner variable
  • Declare variable of type Scanner
  • Named console, refers to monitor and keyword
  • Syntax
  • static Scanner console new Scanner(System.in)
  • Scanner is type of object.
  • new keyword specify creating new instance of
    object
  • System.in specifies input comes from the keyboard
  • static keyword explained later

19
Scanner variable
  • Console
  • In this case, should be a global variable
  • One object used throughout program to get input
    from keyboard
  • public class InputExample
  • static Scanner console new Scanner(System.in)
  • public static void main(String args)
  • // start program

20
Step 3 Import Java objects
  • Import specifies objects required for program
  • Java has 3300 objects
  • Loading all would require unnecessary memory
  • Import listed at top of program
  • Import objects not part of Java lang (System,
    String)
  • Package Collection of objects (java.util)
  • Package name indicates object grouping
  • Example Scanner in util pakage of Java package
  • import java.util.Scanner // also could be import
    java.util.
  • public class InputExample
  • static Scanner console new Scanner(System.in)
    ...

21
Step 4 Use the Scanner object
  • Read input from the keyboard
  • Scanner commands (some)
  • console.nextInt() return integer
  • console.nextDouble() return double
  • console.next() return string
  • Notes
  • Read input up to the next white-space
  • Assigned variable must be same type
  • public static void main(String args) // Start
    program
  • int myInt
  • System.out.println("Enter an integer")
  • myInt console.nextInt() // read input from
    keyboard
  • System.out.println(Answer myInt)

22
Control structures
  • Control structures
  • If-else statement
  • Switch statement
  • For loop
  • While loop
  • We are in luck
  • Almost the same as JavaScript!

23
Boolean logic, same as JavaScript
  • Boolean values
  • true, false (keywords, lower case)
  • Operators
  • and
  • or
  • ! equal/ not equal
  • greater/less than
  • Boolean variable
  • boolean valid false

24
If-else statement
  • Same syntax as JavaScript
  • if (boolean condition) else
  • Use of to group statements
  • Same indentation
  • Applies to primitive data types
  • int, double, boolean, char
  • boolean valid false
  • if (valid true)
  • System.out.println("Yes")
  • else
  • System.out.println(No")

25
Java specific
  • Comparing objects
  • Possible in JavaScript
  • var s1 one string
  • var s2 same as other
  • If (s1 s2)
  • In Java, comparing objects (like a string) is
    different

26
Java specific
  • In Java, comparing objects (like a string) is
    different
  • String s1 one string
  • String s2 other string
  • Incorrect in Java
  • If (s1 s2)
  • Compare objects, Use equals function
  • true if strings match char for char
  • if (s1.equals(s2) true)

27
Compare objects
  • Use comparison function
  • String s1 one string
  • String s2 other string
  • if (s1.compareTo(s2) 0)
  • s1.compareTo(s2) 0 s1 and s2 are equal
  • s1.compareTo(s2)
  • s1.compareTo(s2) 0 s2 before s1
  • See Java String documentation, search web

28
For loop
  • Same syntax as JavaScript
  • int i
  • System.out.println(Start)
  • for (i1 i
  • System.out.println("i " i)
  • System.out.println(Blast off)

Output Start i 1 i 2 i 3 i 4 i 5 i 6 i 7 i 8 i
9 i 10 Blast off
29
Shortcut For loop
  • for loop shortcut (in Java)
  • Declare counter variable in for loop
  • Scope of counter only valid in for loop
  • Easier to manage temporary variable
  • // before variable i unknown, out of scope
  • for (int i1 i
  • // variable i valid in scope of loop
  • System.out.println("i " i)
  • // after variable i unknown, out of scope

30
While loop
  • Same syntax as JavaScript
  • int j 10
  • while (j 0)
  • System.out.println("j " j)
  • j j-2

Output j 10 j 8 j 6 j 4 j 2 j 0
31
Exercise
  • Task
  • Ask the user to input 5 integers.
  • Sum the numbers and display the sum
  • Tips
  • Use a for loop to count the integers
  • Use the Scanner object to get user input
  • Use System.out.println to display messages

32
Solution
  • import java.util.Scanner
  • public class InputExample
  • static Scanner console new Scanner(System.in)
  • public static void main(String args)
  • int sum 0 // Start of program
  • int x
  • for (int i1 i
  • System.out.println("enter number " i)
  • x console.nextInt()
  • sum sum x
  • System.out.println("Sum is " sum)

33
Using Eclipse
  • Run program
  • Compile errors
  • Mistakes while writing program
  • Runtime errors
  • Problems while program executes
  • Java Files

34
Run program
  • Run for first time
  • Select the class in Package Explorer
  • Right click, select Run
  • Run again
  • Click the run button (arrow, green curcle)

35
Compile errors
  • All compile errors must be resolved before
    running program
  • Errors listed next to X icon (missing semicolon)
  • List of errors See Problems tab

36
Compile errors
  • Common errors
  • Missing semicolon
  • Variable not declared or misspelled
  • Incorrect type assigned
  • Compiler errors can be helpful
  • Makes sure program is complete before running
  • Compiler messages can be confusing

37
Runtime errors
  • Runtime errors
  • Problems while program executes
  • Causes program to crash (stop)
  • Examples
  • Divide by zero, invalid computation
  • Scanner.nextInt() crashes when keyboard input is
    not an integer (float or string)

38
Runtime error
  • Scanner.nextInt() crashes when keyboard input is
    not an integer (float or string)
  • Indicates
  • line of code where error occurred (line 31)
  • Type of error, cryptic but useful
  • enter number 1
  • 2
  • enter number 2
  • string
  • Exception in thread "main" java.util.InputMismatch
    Exception
  • at java.util.Scanner.throwFor(Unknown Source)
  • at java.util.Scanner.next(Unknown Source)
  • at java.util.Scanner.nextInt(Unknown Source)
  • at java.util.Scanner.nextInt(Unknown Source)
  • at InputExample.main(InputExample.java31)

39
Java Files
  • Where is my program
  • Java files saved in workspace\project folder
  • Need Java files to submit program
  • Need Java files to share program with others

40
Java Files
  • Workspace\project folder
  • Right click on project in Package Explorer
  • Select Properties
  • Copy the location (highlight text and copy)
  • Open the folder

41
Project properties
  • Workspace\Project folder. Example
  • C\....\workspace\ProjectName

42
Java files
  • Overall Project
  • C\....\workspace\ProjectName
  • Class name ExampleInput
  • Java souce code files (text you type)
  • C\....\workspace\ProjectName\src
  • ExampleInput.java
  • Java byte-code, the file Java uses to run the
    program (bin stands for binary)
  • C\....\workspace\ProjectName\bin
  • ExampleInput.class

43
Fridays recitation
  • Practice Java
  • Input, ouput
  • Loops
  • Using Eclipse
  • Using Java
  • Submitting Java program
Write a Comment
User Comments (0)
About PowerShow.com