102 – Introduction to WWW- http://www.cs.uchicago.edu/info/ services/account_request will need a CS unix account Textbook vitch: JAVA - an Introductio uter Science and Programming Daniel Stefankovic – Ry165 v v nstructor: TA: Xuehai Zhang – Ry256 [email protected][email protected]
63
Embed
CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
ATM should have depositMoney method.1a) What are its parameters? 1b) What is the type of its return value?Assume that Purse has getMoney method, Money getMoney(int amount)
1c) What statement would deposit $300 from your purse to your account?
public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); g.drawLine(0,0,99,99); g.drawLine(0,99,99,0); }}
The result
EXERCISE #3: Modify the paint method of the FirstApplet class to look as follows:
public void paint(Graphics g) { g.drawString("Hello!",20,50); ???? }
public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); g.drawLine(0,0,99,0); g.drawLine(99,0,99,99); g.drawLine(99,99,0,99); g.drawLine(0,99,0,0); }}
More complicated programs
Input Output
3,4 7
Create 3 containers that can hold numbers,the containers are labeled firstNumber,secondNumber and sum.Ask the user for the first number and putit in the container firstNumber.Ask the user for the second number and putit in the container secondNumber. Compute the sum of the numbers in containers firstNumber and secondNumberand store it in sum.Output the content of sum.
The Sum program
import javax.swing.*;
public class Sum { public static void main(String args[]) {
int firstNumber,secondNumber,sum;
firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:"));
sum=FirstNumber+SecondNumber; JOptionPane.showMessageDialog(null,"The sum is " + sum);
System.exit(0); }}
The Sum program
import javax.swing.*;
public class Sum { public static void main(String args[]) {
int firstNumber,secondNumber,sum;
firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:"));
sum=FirstNumber+SecondNumber; JOptionPane.showMessageDialog(null,"The sum is " + sum);
System.exit(0); }}
we can have variableswhich contain a number
type,identifier
The Sum program
import javax.swing.*;
public class Sum { public static void main(String args[]) {
int firstNumber,secondNumber,sum;
firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:"));
sum=FirstNumber+SecondNumber; JOptionPane.showMessageDialog(null,"The sum is " + sum);
System.exit(0); }}
we can take user input and store it in a variable
we can output content of a variable
The Sum program
import javax.swing.*;
public class Sum { public static void main(String args[]) {
int firstNumber,secondNumber,sum;
firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:"));
sum=FirstNumber+SecondNumber; JOptionPane.showMessageDialog(null,"The sum is " + sum);
System.exit(0); }}
The Sum program we can compute something using values in variables and store the result in a variable
expression,assignment
Type =
int firstNumber,secondNumber,sum;
what kind of thingscan be stored in a variable
int = integer in range –2147483648,+2147483647
similarly byte,short,long
float = floating point number (“real number”)
10 +1=10 20 20
similarly double
Identifier = the name of a variable
Any sequence of letters and digits,starting with a letter, except keywords.
abstract else interface super boolean extends long switch break final native synchronized byte finally new this case float null throw catch for package throws char goto private transient class if protected try const¡ù implements public void continue import return volatile do instanceof short while double int static
Convention: the first letter lowercase
2ndSum
Expressionsmass*velocity*velocity
(a+b)*(a-b)
1/(1-q)
Operators+,-,*,/,%
q
a%5
expression = variable | constant | expression op expression
Expressions expression = variable | constant | expression op expression
Expressions have type!
int a,b; a+b,a*b,a-b,a/b int
float a,b; a+b,a*b,a-b,a/b float
56 int21.2 double
21.2f float
Expressions expression = variable | constant | expression op expression
Expressions have type!
int b; a+b,a*b,a-b,a/b floatfloat a;
behaves as if b were float anything of type int “fits” in float
Expressions expression = variable | constant | expression op expression
Expressions have type!
int b; a+b,a*b,a-b,a/b floatfloat a;
behaves as if b were float anything of type int “fits” in float
Casting (float) b
Expressions expression = variable | constant | expression op expression
Expressions have type!
anything of type int “fits” in float
byte->short->int->long->float->double
a op b
Expressions expression = variable | constant | expression op expression
Expressions have type!
The behavior of some operators depends on the type of operands!
a=7; b=2; a/b is 3 if both a,b are int
a/b is 3.5 if a or b or both are float
EXERCISE #4Let a=7,b=2,c=2
What is the type and value of (a/b)/c
in the following cases1.int a,b,c;2.int a,b; float c;3.int a; float b,c;4.float a,b,c;