cs 102 – introduction to services/account_request you will need a cs unix account textbook...
Post on 22-Dec-2015
218 views
TRANSCRIPT
![Page 1: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/1.jpg)
CS 102 – Introduction to WWW-2
http://www.cs.uchicago.edu/info/services/account_request
You will need a CS unix account
TextbookW.Savitch: JAVA - an Introduction to Computer Science and Programming
Daniel Stefankovic – Ry165Av v
instructor:
TA: Xuehai Zhang – [email protected]
![Page 2: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/2.jpg)
CS 102 Homeworks, powerpoint presentations
available from the class webpagepeople.cs.uchicago.edu/~stefanko
Homeworks – due Friday 9:00pm
1. download all files to a directory e.g. hw12. solve the problems3. submit the solutions using
hwsubmit cs102 ~/hw1/
![Page 3: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/3.jpg)
CS102
Mailing List
Office hoursFriday 6:30-8:00pm, Ry256
http://mailman.cs.uchicago.edu/mailman/listinfo/cs102
subscribe at:
![Page 4: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/4.jpg)
CS102 Grading
20% - Homeworks40% - Midterm40% - Project
Midterm08/22, 10:30-12:30, open book
![Page 5: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/5.jpg)
CS102 Project
due 08/29, 10:30am
topic?
![Page 6: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/6.jpg)
What is Java?
Object oriented language.
![Page 7: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/7.jpg)
Object oriented language.
The world around us consists of objects.
e.g. the ATM in Reynolds club
![Page 8: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/8.jpg)
Object oriented language.
The world around us consists of objects.Let the program consist of objects.
![Page 9: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/9.jpg)
Object oriented language.
The program consist of objects.
Objects of the same kind form a class.
E.g. class ATM or class Money.
![Page 10: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/10.jpg)
Object oriented language.
Each object has some methods.Money withdrawMoney(ATMCard card,int amount)
The program consist of objects.Objects of the same kind form a class.
(Objects in the same classhave the same methods.)
![Page 11: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/11.jpg)
Object oriented language.
Money withdrawMoney(ATMCard card,int amount)
A method of the ATM class: parameters
type of return value
myPurse.addMoney(theATMInReynolds.withdrawMoney(myATMCard,1000));
type of the parametername of the parameter
![Page 12: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/12.jpg)
EXERCISE #1:
Money withdrawMoney(ATMCard card,int amount)
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?
myPurse.addMoney(theATMInReynolds.withdrawMoney(myATMCard,1000));
![Page 13: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/13.jpg)
SOLUTION #1:1a) Card and Money1b) nothing void
void depositMoney(ATMCard card,Money money)
1c)
theATMInReynolds.depositMoney(myCard,myPurse.getMoney(300));
![Page 14: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/14.jpg)
Object oriented language.
The world around us consists of objects.Let the program consist of objects.
more ideas borrowed from the real world:
encapsulation – you do not need to know how the ATM works inside.
![Page 15: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/15.jpg)
Object oriented language.
The world around us consists of objects.Let the program consist of objects.
more ideas borrowed from the real world:
inheritance – you can easily createclass ATMWithClocks extending classATM. The new class inherits the methods of the ATM class.
![Page 16: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/16.jpg)
What is Java?
Object oriented language.
![Page 17: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/17.jpg)
What is Java?
Object oriented language.
Java virtual machine.Java platform.
a piece of software(interpreter)
collection of useful classes
+
![Page 18: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/18.jpg)
What is Java?
Compiler
source code
byte code
JVM
Computer
programmer user
![Page 19: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/19.jpg)
What is Java?
Compiler
source code
byte code
JVM
Computer
programmer user
portabilitysecurity
speed
![Page 20: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/20.jpg)
Why Java? • simple• portable• secure• free
• slow
![Page 21: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/21.jpg)
Two kinds of Java programs
applicationsapplets
a small application that can be displayed on a web page
Java Applet Example
![Page 22: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/22.jpg)
Learning Java
• language• libraries
book, lectures
documentation
examples on the web(problem – often old version of Java)
http://java.sun.com/docs/
![Page 23: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/23.jpg)
The first applet
import java.applet.Applet;import java.awt.Graphics;
public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}
![Page 24: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/24.jpg)
The first applet
import java.applet.Applet;import java.awt.Graphics;
public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}
what to draw where to draw it
![Page 25: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/25.jpg)
Viewing the applet 1. compile
javac FirstApplet.java
2. insert following tag in a web page<APPLET CODE="FirstApplet.class" WIDTH="100" HEIGHT="100"></APPLET>
3. view the webpage
![Page 26: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/26.jpg)
Importing packages
import java.applet.Applet;import java.awt.Graphics;
public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}
Classes are grouped in packages.
E.g. Applet is in java.applet.
![Page 27: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/27.jpg)
Importing packages
import java.applet.Applet;import java.awt.Graphics;
public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}
Importing a package allows you to use shorter name of the class.
java.applet.Appletjava.awt.Graphics
![Page 28: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/28.jpg)
Extending existing classes
import java.applet.Applet;import java.awt.Graphics;
public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}
FirstApplet is an Applet (inheritance).
What did we inherit?
![Page 29: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/29.jpg)
Implementing methods
import java.applet.Applet;import java.awt.Graphics;
public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}
We modify the paint method.
![Page 30: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/30.jpg)
Implementing methods
import java.applet.Applet;import java.awt.Graphics;
public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}
The body of a method consist of a sequence of statements.
![Page 31: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/31.jpg)
The important part
import java.applet.Applet;import java.awt.Graphics;
public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}
drawString is a method of theGraphics class. It has some parameters.
Documentation for Graphics class
![Page 32: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/32.jpg)
The coordinate system The applet is drawn in a rectangle, which consists of pixels.
width
height
![Page 33: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/33.jpg)
The coordinate system Each pixel has a coordinate (x,y)
x
y
(0,0) (width-1,0)
(0,h
eigh
t-1)(w
idth-1,height-1)
![Page 34: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/34.jpg)
EXERCISE #2:Let width and height be odd. Whatare the coordinates of the middlepixel?
![Page 35: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/35.jpg)
SOLUTION #2:width = 3, height = 3answer = (1,1)
answer = ((width-1)/2,(height-1)/2)
/ 2 , / 2answer width height
answer = (width/2,height/2)
![Page 36: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/36.jpg)
Using the documentation
g.drawString("Hello!",20,50);
Now we understand
Documentation for Graphics class
![Page 37: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/37.jpg)
A modification
import java.applet.Applet;import java.awt.Graphics;
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); }}
![Page 38: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/38.jpg)
The result
![Page 39: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/39.jpg)
EXERCISE #3: Modify the paint method of the FirstApplet class to look as follows:
public void paint(Graphics g) { g.drawString("Hello!",20,50); ???? }
![Page 40: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/40.jpg)
SOLUTION #3: import java.applet.Applet;import java.awt.Graphics;
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); }}
![Page 41: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/41.jpg)
More complicated programs
Input Output
3,4 7
![Page 42: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/42.jpg)
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
![Page 43: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/43.jpg)
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
![Page 44: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/44.jpg)
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
![Page 45: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/45.jpg)
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
![Page 46: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/46.jpg)
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
![Page 47: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/47.jpg)
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
![Page 48: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/48.jpg)
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
![Page 49: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/49.jpg)
Expressionsmass*velocity*velocity
(a+b)*(a-b)
1/(1-q)
Operators+,-,*,/,%
q
a%5
expression = variable | constant | expression op expression
![Page 50: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/50.jpg)
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
![Page 51: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/51.jpg)
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
![Page 52: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/52.jpg)
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
![Page 53: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/53.jpg)
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
![Page 54: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/54.jpg)
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
![Page 55: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/55.jpg)
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;
![Page 56: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/56.jpg)
SOLUTION #4 a=7,b=2,c=2(a/b)/c1. int a,b,c;
(7/2)/2 -> (3)/2 -> 12. int a,b; float c;
(7/2)/2 -> (3)/2 -> 1.53. int a; float b,c; float a,b,c;
(7/2)/2 -> (3.5)/2 -> 1.75
![Page 57: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/57.jpg)
Assignmentsvariable = expression
The expression type must “fit in” thevariable type.
int a,b;float c; a=b+c;
![Page 58: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/58.jpg)
Assignmentsvariable = expression
The expression type must “fit in” thevariable type.
int a,b;float c; a=b+c;
![Page 59: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/59.jpg)
Assignmentsvariable = expression
The expression type must “fit in” thevariable type.
int a,b;float c; a=b+c;
a=(int)(b+c)
Casting will cause truncation
![Page 60: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/60.jpg)
Assignmentsvariable = expression
The expression type must “fit in” thevariable type.
int a,b;float c;
a=b+c;
a=(int)(b+c)
Casting will cause truncationb=2, c=2.7 -> (int)(b+c)=4
![Page 61: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/61.jpg)
EXERCISE #5int a,b,c;float d;
You want to put the average of a,b,c to d.
![Page 62: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/62.jpg)
SOLUTION #5int a,b,c;float d;
You want to put the average of a,b,c to d.
d=(a+b+c)/3;
d=(a+b+c)/3.0;
d=(a+b+c)/3.0f;
d=(float)(a+b+c)/3; ?
![Page 63: CS 102 – Introduction to services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d795503460f94a5cb38/html5/thumbnails/63.jpg)
SOLUTION #5int a,b,c;float d;
You want to put the average of a,b,c to d.
d=(a+b+c)/3;
d=(a+b+c)/3.0;
d=(a+b+c)/3.0f;
d=(float)(a+b+c)/3;