Download basics - U of W ACS Homepage

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Literals, Variables, Data types
• Constants in Java are called literals.
•
•
•
•
•
•
•
•
123
123.45
'a'
'&‘
"Gosling“
"this is a line“
true
false
Int
double
char
String
boolean
Later on we’ll see another way to define constants…..
1
Literals, Variables, Data types
Variables
• Named location in memory
• Program controls its contents:
• Initialize, change, display, access
• Java is strongly typed
• You must declare the type of each variable … cannot
change this later
2
Literals, Variables, Data types
Data types: 8 primitive types
Need to know int, double, char, boolean
Numeric
int
long
float
double
char
boolean
100
234
0
100L
234L 0L
100.12f 234.0f 0.0f
100.12 234.0 0.0
'a', 'b', … 'A', 'B',… '0', '1', '2', … '~', '@', '#', …
true, false
int and double are default types for numeric literals.
For numeric types: we will focus on int and double.
3
Literals, Variables, Data types
Integer numeric types vary according to
the amount of memory
smallest/largest values
4
Literals, Variables, Data types
Integer numeric types
Multiplication
Division
Modulo
Sample program : IntegerArithmetic.java
5
Literals, Variables, Data types
Decimal numeric types
6
Literals, Variables, Data types
Decimal numeric types
10.0 / 4.0
Correction to page 26
10.0/4.0 is 2.5
10/4 is 2
Sample program : FuelConsumption.java
7
Literals, Variables, Data types
Decimal – values are often approximations
Sample program : Approximations.java
8
boolean
boolean type has 2 values: true, false
Operators && || !
boolean xyz = true ;
boolean found ;
If (xyz) System.out.println("the variable is true");
If (xyz && found ) …………….
9
char
char is used for single characters
Usual operators <
<= == !=
>
>=
char a, b, c;
a = '*';
b = 'q';
c = '1';
If (a == b) ……………
To compare two things
for equality we normally
use ==
= is discussed much later
10
String
Literals enclosed in double quotes
"this is a line of text"
"any character such as $ % * . < etc can be included"
Variables
Declared as a String type
e.g.
String firstName, lastName, address;
firstName = "Jones";
firstName.length() ;
// see next page
11
String
Memory for Strings is handled differently from primitive data types
Primitive
type
Object
12
String
Many useful methods for handling String data
13
System.out.println(…)
Used to start a new line of output on the Terminal Window
What is output for:
public static void main (){
System.out.println("123");
System.out.println("456");
System.out.println("789");
System.out.println("0");
}
14
System.out.println(…)
Used to complete a line of output on the Terminal Window
What is output for:
public static void main (){
System.out.println("123");
System.out.println("456");
System.out.println("789");
System.out.println("0");
}
15
System.out.print(…)
Used to continue a line displayed on the Terminal Window
What is the output for:
public static void main (){
System.out.print("123");
System.out.print("456");
System.out.print("789");
System.out.print("0");
}
16
System.out.print(…)
Used to continue a line displayed on the Terminal Window
What is the output for:
public static void main (){
System.out.print("123");
System.out.print("456");
System.out.print("789");
System.out.print("0");
}
17
System.out….(…)
Used to continue a line displayed on the Terminal Window
What is the output for:
public static void main (){
System.out.println("start of display");
System.out.print("123");
System.out.println("456");
System.out.print("789");
System.out.println("0");
System.out.println("end of display");
}
18
System.out….(…)
Used to continue a line displayed on the Terminal Window
What is the output for:
public static void main (){
System.out.println("start of display");
System.out.print("123");
System.out.println("456");
System.out.print("789");
System.out.println("0");
System.out.println("end of display");
}
19
Expressions
Complex – more
than one operator
20
Expressions
If there is more than one operator at the same priority the evaluation goes from
left to right.
Suppose c=2.0;
9.0 / 5.0 * c + 32.0
1.8
3.6
35.6
21
Expressions
Suppose c=2.0;
Like the previous example, but what if the addition is written first:
32.0 + 9.0 / 5.0 * c
1.8
3.6
35.6
/ and * have higher priority
than +
Where priority is same,
evaluate from left to right
22
Expressions
Priorities (and so order of evaluation) can be overridden with a subexpression …
operations enclosed in parentheses
A sub-expression is always evaluated before the expression in which it is
contained is evaluated. Of course the sub-expression is evaluated
according to the rules of expressions.
23
Expressions
A sub-expression is always evaluated before the expression in
which it is contained is evaluated
Suppose c=2.0;
Consider the following:
(9.0 / (5.0 * ( c + 32.0 ) ) )
34.0
170.0
0.0529
24
Mixed Mode Expressions
An expression that involves ints and doubles is a mixed-mode expression
Example: 9 / 5.0 * 2 + 32
If an operation involves two operands where one is an int and the other is
a double, then the int is converted automatically to its double
equivalent before the operation occurs.
9 / 5.0 * 2 + 32
1. The first operation is “/” … the 9 is converted to 9.0 and we have
9.0/5.0 * 2 + 32
And so we have
1.8 * 2 + 32
2. “*” is performed next … the 2 is converted to 2.0
1.8 * 2.0 + 32
And so we have
3.6 + 32
3. 32 becomes 32.0 and we have the final result of 35.6
25
Mixed Mode Expressions
Example: 9 / 5 * 2 + 32.0
5 and not 5.0
9 / 5 * 2 + 32.0
1. The first operation is “/” … 9/5 is 1
And so we have
1 * 2 + 32.0
2. “*” is performed next
And so we have
2 + 32.0
3. 2 becomes 2.0 and we have the final result of 34.0
26
Expressions
Suppose we need to determine an employee’s net pay where this is calculated
as:
Step 1: subtract deductions from gross pay, and multiply that result by tax
rate giving taxes paid
Step 2: subtract taxes paid and deductions from gross pay giving net pay
Example:
Gross pay is $10,000
Deductions are $1,500
Tax rate is 20%
Taxes paid are ($10,000 – $1,500) * 20% yields $1,700
Net pay is $10,000 - $1,500 - $1,700 yields $6,800
Note that at the bottom of page 30 we have expressions
grossPay - deductions * taxRate
(grossPay - deductions) * taxRate
both of which are wrong …
27
Expressions
How do we code this in Java ? How do we determine netPay?
netPay = grossPay - grossPay - deductions * taxRate;
or
netPay = grossPay - (grossPay-deductions) * taxRate;
or
netPay = grossPay - deductions - (grossPay - deductions) * taxRate;
or
… perhaps do this in a few steps:
netPayLessDeductions = grossPay – deductions;
taxesPaid = netPayLessDeductions * taxRate;
netPay = netPayLessDeductions – taxesPaid;
28
Expressions
 Program to calculate net pay given the user supplies gross pay,
deductions, and tax rate.
Examine the Scanner class first
29
Expressions
Area of a Trapezoid
How do we express the calculation of area in Java?
area = a + b / 2 h
or
area = a + b / 2 * h
or
area = (a + b) / 2 * h
or
area = ((a + b) / 2) * h
30
Expressions
We will write a program to calculate
1. Net pay (slide 34)
2. Area of a trapezoid (to do in class)
but where the user supplies the necessary values.
But first, to handle input, we examine the Scanner class first
31
Input Using the Scanner Class
•System - a pre-defined Java class that has an object named in.
•The standard input stream is System.in.
•A typical statement is:
Scanner keyboard = new Scanner(System.in);
•The keyboard object above is a Scanner object to be used to
get user input from the keyboard
•The Scanner class is defined in the Java library named java.util
 need an import statement for this
import java.util.Scanner;
32
Input Using the Scanner Class
33
Sample program – calculate gross pay
Taxes paid are (gross pay minus deductions) * tax rate
Net pay is gross pay minus deductions minus taxes
import java.util.Scanner;
public class NetPayCalculation
{
public static void main(String[] args)
{
double grossPay, deductions, taxRate, taxes, netPay;
String name;
// Declare a scanner object for the keyboard
Scanner keyboard = new Scanner(System.in);
// Prompt the user for a name
System.out.println("\n\nEnter your name (no spaces) and press enter");
name = keyboard.next();
Continued 
34
Sample program – calculate gross pay
// Prompt the user for gross pay
System.out.println( "Enter the gross pay");
grossPay = keyboard.nextDouble();
// Prompt the user for deductions
System.out.println("Enter the deductions");
deductions = keyboard.nextDouble();
// Prompt the user for tax rate
System.out.println( "Enter the tax rate");
taxRate = keyboard.nextDouble();
// Calculate net pay and display all the information
taxes = (grossPay - deductions) * taxRate;
netPay = grossPay - deductions - taxes;
System.out.println("\n Your name: "+name
+"\n gross pay: "+ grossPay
+"\n deductions: "+ deductions
+"\n taxRate: "+taxRate
+"\n taxes: "+taxes
+"\n net pay: "+netPay);
}
}
35
Sample program – calculate area of trapezoid
import java.util.Scanner;
public class TrapezoidAreaCalculation
{
public static void main(String[] args)
{
}
}
36
JOptionPane – for input and output
Just another way to manage input and output
User interacts with program through pop-up windows
User must click OK for the program to continue
37
JOptionPane – for input and output
import javax.swing.JOptionPane;
public class HelloWorld
{
public static void main(String[] args)
{
String message = "Hello World";
JOptionPane.showMessageDialog (null, message);
}
}
User sees
showMessageDialog
to display information
38
JOptionPane – for input and output
import javax.swing.JOptionPane;
public class HelloYou
showInputDialog
{
to get information
public static void main(String[] args)
{
String message = "What is your name?";
String firstName = JOptionPane.showInputDialog (" Enter your name: ") ;
JOptionPane.showMessageDialog (null, " Hello "+firstName) ;
}
}
User sees
39