comp 110-001 mid-term reviewcobweb.cs.uga.edu/~yihong/comp110/files/slides/lecture13_may27.pdfclass...
TRANSCRIPT
![Page 1: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/1.jpg)
COMP 110-001Mid-Term Review
Yi Hong May 27, 2015
![Page 2: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/2.jpg)
Announcement § Midterm on Friday, May 29 • Closed books, no notes, no computer
![Page 3: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/3.jpg)
Today § A whirlwind tour of almost everything we
have covered so far
§ You should start preparing for mid-term if you haven’t
§ Finish the mid-term practice before Thursday § Review slides and textbook § Review your lab / assignment code
![Page 4: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/4.jpg)
Hardware vs. Software § Hardware - physical machine • CPU, Memory
§ Software - programs that give instructions to the computer • Windows XP, Games, Eclipse
4!
![Page 5: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/5.jpg)
Hardware § CPU – the “brain” of your computer § Memory – stores data for the computer • How much the “brain” can remember • Main memory: RAM • Auxiliary memory: Hard Drive
5!
![Page 6: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/6.jpg)
Memory § Measured in bytes § 1 byte = 8 bits § Bit is either 0 or 1 § Language of the computer is in bits
6!
![Page 7: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/7.jpg)
Programming Languages
7!
Your Program!
Compiler!
Machine Language (Bits)!
High-level language !(human readable)!
Low-level language!(computer readable)!
![Page 8: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/8.jpg)
Algorithms and Pseudocode § Algorithm – a set of instructions for solving
a problem
§ Pseudocode – combination of code and English used to express an algorithm before writing algorithm into code • We can also use flow-chat to write
pseudocode
8!
![Page 9: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/9.jpg)
Variables § Used to store data in a program § The data currently in a variable is its value § Name of variable is an identifier • Letters, digits, underscore • Cannot start with digits
§ Can change value throughout program § Choose variable names that are
meaningful!
9!
![Page 10: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/10.jpg)
How to Use Variables § Declare a variable • int number;
§ Assign a value to the variable • number = 37;
§ Change the value of the variable • number = 513;
10!
![Page 11: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/11.jpg)
Keywords § Reserved words with predefined meanings § You cannot name your variables keywords § if, else, return, new
11!
![Page 12: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/12.jpg)
Data Type § What kind of value the variable can hold § Two kinds of types. • Primitive type - indecomposable values
• Names begin with lowercase letters • int, double, char, float, byte, boolean, and others
• Class type - objects with both data and methods • Names by convention begin with uppercase letter • Scanner, String, Student
12!
![Page 13: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/13.jpg)
Assignment Statements § Change a variable’s value § Syntax • variable = expression;
§ Example • sleepNeeded = 8; • sleepDesired = sleepNeeded * 2;
13!
![Page 14: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/14.jpg)
Assignment Compatibilities int x = 5; double y = 12.7; y = x; à = x = y; à =
14!
OK
Not OK
byte à short à int à Long à float à double
![Page 15: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/15.jpg)
Type Casting x = (int) y; à =
15!
(int) OK
![Page 16: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/16.jpg)
Arithmetic Operators § Unary operators • +, -, ++, --, !
§ Binary arithmetic operators • *, /, %, +, -
• rate*rate + delta • 1/(time + 3*mass) • (a - 7)/(t + 9*v)
16!
![Page 17: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/17.jpg)
Modular Arithmetic: % § Remainder § 7 % 3 = 1 (7 / 3 = 2, remainder 1) § 8 % 3 = 2 (8 / 3 = 2, remainder 2) § 9 % 3 = 0 (9 / 3 = 3, remainder 0)
17!
![Page 18: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/18.jpg)
Parentheses and Precedence § Expressions inside parentheses evaluated first • (cost + tax) * discount • cost + (tax * discount)
§ Precedence rules Highest Precedence
• First: the unary operators +, -, !, ++, and -- • Second: the binary arithmetic operators *, /, % • Third: the binary arithmetic operators + and –
Lowest Precedence
18!
![Page 19: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/19.jpg)
Errors § Syntax error – grammatical mistake in your
program • Java will not compile programs with syntax
error
§ Run-time error – an error that is detected during program execution • E.g., exceptions during execution
§ Logic error – a mistake in a program caused by the underlying algorithm
19!
![Page 20: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/20.jpg)
Strings § A string (lowercase) is a sequence of
characters • “Hello world!” • “Enter a whole number from 1 to 99.”
§ String (capital S) is a class in Java, not a primitive type
20!
![Page 21: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/21.jpg)
String String animal = “aardvark”; System.out.println(animal); aardvark
21!
![Page 22: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/22.jpg)
String Concatenation String animal = “aardvark”; String sentence; sentence = “My favorite animal is the ” + animal; My favorite animal is the aardvark
22!
![Page 23: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/23.jpg)
String’s Methods § myString.length(); § myString.equals(“a string”); § myString.toLowerCase(); § MyString.indexOf(‘ ’); § myString.trim(); § …
§ For other methods, check Java API
23!
![Page 24: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/24.jpg)
String Indices
24!
U" N" C" i" s" G" r" e" a" t"0" 1" 2" 3" 4" 5" 6" 7" 8" 9" 10" 11"
String output = myString.substring(1, 8);
![Page 25: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/25.jpg)
String Indices
25!
U" N" C" i" s" G" r" e" a" t"0" 1" 2" 3" 4" 5" 6" 7" 8" 9" 10" 11"
String output = myString.substring(1, 8);
![Page 26: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/26.jpg)
Escape Characters
26!
\”" Double quote"\’" Single quote"\\" Backslash"\n" New line"\r" Carriage return"\t" Tab"
![Page 27: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/27.jpg)
Keyboard Input Scanner keyboard = new Scanner(System.in); int num = keyboard.nextInt();
27!
![Page 28: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/28.jpg)
Comments // this is a comment
/* This is also
a comment.
it ends
here -‐-‐-‐>*/
28!
![Page 29: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/29.jpg)
Boolean Expressions § An expression that is either true or false § Examples: • It is sunny today (true) • 10 is larger than 5 (true) • Today is Saturday (false)
29!
![Page 30: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/30.jpg)
if/else Statements
30!
Is input greater than 10?
Yes No
Prompt user for integer
Print: “big
number”
Print: “small
number”
import java.util.*; public class FlowChart { public static void main(String[] args) { System.out.println("Give me an integer:"); Scanner keyboard = new Scanner(System.in); int inputInt = keyboard.nextInt(); if (inputInt > 10) { System.out.println("big number"); } else { System.out.println("small number"); } } }
![Page 31: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/31.jpg)
If-else-if for Multi-Branch Selections if ( case1 ) {
// branch 1
} else if ( case2) {
// branch 2
} else if ( case3 ) { …
…
} else {
…
}
if (year==1) { System.out.println(“Freshman”);
} else if (year==2) { System.out.println(“Sophomore”);
} else if (year==3) { System.out.println(“Junior”);
} else { System.out.println(“Senior”);
}
![Page 32: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/32.jpg)
Java Comparison Operators for Primitive Values
32!
== Equal to != Not equal to > Greater than >= Greater than or equal to
< Less than <= Less than or equal to
Example expressions: variable <= 6 myInt > 5 5 == 3
The result is a boolean value (true/false)
![Page 33: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/33.jpg)
Boolean Type § Can be either true or false
boolean sunny = true;
boolean cloudy = false;
if (sunny || cloudy)
{
// walk to school
}
33!
![Page 34: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/34.jpg)
&&, || operators § AND
if ((temperature > 50) && (temperature < 75)) { // walk to school
}
§ OR if (sunny || cloudy) { // walk to school
}
34!
![Page 35: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/35.jpg)
The ! (NOT) operator § !true is false § !false is true § Example: walk to school if it is NOT cloudy
if (!cloudy)
{
// walk to school
}
35!
![Page 36: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/36.jpg)
Loops § Loop: part of a program that
repeats
§ Body: statements being repeated
§ Iteration: each repetition of body
§ Stopping condition 36!
Start
Enough sandwiches?
Distribute sandwiches
No!Yes!Make
sandwich
![Page 37: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/37.jpg)
Types of Loops § while • Safest choice • Not always the best
§ do-while • Loop iterates AT LEAST once
§ for • Similar to while, but often more convenient syntax • Most useful when you have a known number of
iterations you need to do
37!
![Page 38: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/38.jpg)
Using a while Loop
int n = 1;
while (n <= 10)
{
System.out.println(n);
n = n + 1;
}
38!
n=1
n <= 10?
EndOutput n
No
n=n+1
Yes
![Page 39: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/39.jpg)
Using a for Loop
int n;
for (n = 1; n <= 10; n++)
{
System.out.println(n);
}
39!
![Page 40: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/40.jpg)
Infinite Loop Example
int n;
for (n = 1; n <= 10; n = 0)
{
System.out.println(n);
}
40!
![Page 41: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/41.jpg)
The break statement for (int item = 1; item <= 5; item++) { System.out.print(“Enter cost of item #” + item + “: $”); amount = keyboard.nextDouble(); total = total + amount; if (total >= 100) { System.out.println(“You spent all your money.”); break; } System.out.println(“Your total so far is $” + total); } System.out.println(“You spent $” + total);
41!
![Page 42: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/42.jpg)
Ending a Loop § Count-controlled loops • If you know the number of loop iterations • for (count = 0; count < iterations; count++)
§ User-controlled loops • Change the value of control variable
• E.g., Ask-before-iterating, or sentinel value ( if user input is smaller than 0) • E.g., booleans, matching is found
42!
![Page 43: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/43.jpg)
Nested Loops Example
for (int i = 1; i<10; i++) { for (int j = 1; j<=i; j++) { System.out.print( i + ”*” + j + “=“ + (i * j) + “\t“); } System.out.println();
}
43!
Inner loop
Outer loop
![Page 44: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/44.jpg)
Classes, Objects, and Methods § Class: a definition of a kind of object § Object: an instance of a class • Contains instance variables (data) and
methods
§ Methods • Methods that return a value • Methods that return nothing
44!
![Page 45: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/45.jpg)
Class § A class is the definition of a kind of object • A blueprint for constructing specific objects
45!
5.1 Class and Method Definitions 263
5.1 CLASS AND METHOD DEFINITIONS
The greatest invention of the nineteenth century was the invention of the method of invention.
ALFRED NORTH WHITEHEAD, SCIENCE AND THE MODERN WORLD
A Java program consists of objects of various class types, interacting with one another. Before we go into the details of how you define your own classes and objects in Java, let’s review and elaborate on what we already know about classes and objects.
Objects in a program can represent either objects in the real word—like automobiles, houses, and employee records—or abstractions like colors, shapes, and words. A class is the definition of a kind of object. It is like a plan or a blueprint for constructing specific objects. For example, Figure 5.1 describes a class called Automobile. The class is a general description of what an automobile is and what it can do.
Objects in a program can represent real-world things or abstractions
FIGURE 5.1 A Class as a Blueprint
Class description
Second Instantiation:
Object name: suesCar
First Instantiation:
Object name: patsCar
Third Instantiation:Object name: ronsCar
amount of fuel: 14 gallonsspeed: 0 miles per hourlicense plate: "SUES CAR"
amount of fuel: 10 gallonsspeed: 55 miles per hourlicense plate: "135 XJK"
amount of fuel: 2 gallonsspeed: 75 miles per hourlicense plate: "351 WLF"
Class Name: Automobile
Data: amount of fuel__________ speed __________ license plate __________
Methods (actions): accelerate:
How: Press on gas pedal. decelerate:
How: Press on brake pedal.
Objects that are instantiationsof the class Automobile
![Page 46: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/46.jpg)
Objects, Instantiation Object Name: patsCar amount of fuel: 10 gallons speed: 55 miles per hour license plate: “135 XJK”
Object Name: suesCar amount of fuel: 14 gallons speed: 0 miles per hour license plate: “SUES CAR”
Object Name: ronsCar amount of fuel: 2 gallons speed: 75 miles per hour license plate: “351 WLF”
Instantiations, or instances, of the class Automobile
![Page 47: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/47.jpg)
§ Create an object jack of class Student Student jack = new Student();
Scanner keyboard = new Scanner(System.in);
§ Create an object keyboard of class Scanner
Creating an Object
Create an object Return memory address of object
Assign memory address of object to variable
![Page 48: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/48.jpg)
§ Data defined in the class are called instance variables
public String name; public int classYear;
public double gpa; public String major;
Instance Variables
public: no restrictions on how these instance variables are used (more details later – public is actually a bad idea here)
Data type: int, double, String…
variables
![Page 49: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/49.jpg)
§ Two kinds of methods • Methods that return a value
• Examples: String’s substring() method, String’s indexOf() method, etc.
• Methods that return nothing • Perform some action other than returning an item • Example: System.out.println()
49!
Methods
![Page 50: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/50.jpg)
public String getMajor() { return major; } public void increaseYear() { classYear++; }
50!
Methods returns a String
returns nothing
return type
![Page 51: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/51.jpg)
§ Object, followed by dot, then method name, then () • Order, type, and number of arguments must
match parameters specified in method heading
§ Use them as Java statements
Student jack = new Student(); jack.classYear = 1;
jack.increaseYear();
System.out.println(“Jack’s class year is ” + jack.classYear);
51!
Calling Methods That Return Nothing
![Page 52: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/52.jpg)
§ Object, followed by dot, then method name, then () (the same as before)
§ Use them as a value of the type specified by the method’s return type
Student jack = new Student(); jack.major = “Computer Science”;
String major = jack.getMajor();
System.out.println(“Jack’s full name is ” + jack.getName()); System.out.println(“Jack’s major is ” + major);
52!
Calling Methods That Return a Value
![Page 53: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/53.jpg)
§ Instance variables • Declared in a class • Confined to the class • Can be used in any
method in this class
§ Local variables • Declared in a method • Confined to the method • Can only be used inside
the method
53!
Local / Instance Variables public class Student { public String name; public int classYear; public String major; public void printInfo(){ String info = name + “:” + major + “:” + classYear;
System.out.println(info);
} public void increaseYear(int inc) { classYear += inc; } }
![Page 54: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/54.jpg)
An Example public class Student { public String name; public int classYear; public String major; public void printInfo() { String info = name + “: ” + major + “: ” + classYear ; System.out.println(info); } public void increaseYear(int inc) { classYear += inc; } }
54!
• classYear and name are instance variables!• can be used in any method in this class!
• info is a local variable declared inside method printInfo()!• can only be used inside method printInfo()!
![Page 55: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/55.jpg)
public class Student { public String name; public int classYear; public String major;
public void printInfo() { String info = name + “: ” + major + “: ” + classYear ; System.out.println(info); }
public void increaseYear(int inc) {
classYear += inc; info = “info changed a bit”; }
} 55!
An Example
• Java will not recognize info
✗
![Page 56: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/56.jpg)
§ Parameters are used to hold the value that you pass to the method
§ Parameters can be used as (local) variables inside the method
public int square(int number) { return number * number; }
Methods with Parameters
Parameters go inside parentheses of method header
![Page 57: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/57.jpg)
§ Multiple parameters separated by commas
public double getTotal(double price, double tax) { return price + price * tax; }
Methods with Multiple Parameters
![Page 58: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/58.jpg)
public class SalesComputer { public double getTotal(double price, double tax) { return price + price * tax; } // ... } SalesComputer sc = new SalesComputer(); double total = sc.getTotal(“19.99”, Color.RED); double total = sc.getTotal(19.99); double total = sc.getTotal(19.99, 0.065); int price = 50; total = sc.getTotal(price, 0.065);
Method Parameters and Arguments
Automatic typecasting
✗ ✗
✓
✓
![Page 59: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/59.jpg)
Calling Methods from Methods § In a method’s body, we can call another
method • receiving_object.method();
§ If calling a method in the same class, we do not need receiving_object: • method();
§ Alternatively, use the this keyword • this.method();
![Page 60: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/60.jpg)
Several Common Mistakes § Unwanted semicolon after if / for statements if (a>b); // this semicolon causes an empty if-branch
c++; // this line is always executed
for(int i = 0; i<10; i++); // this semicolon indicates an empty loop body
c++; // this is executed only once
§ Unpaired brackets • Use indentation to help checking • Use Eclipse’s auto format function
![Page 61: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/61.jpg)
Indentation § Indentation • Makes code easier to read • Helps with finding syntax and logic errors • Indent code that goes between { and }
§ Be consistent!
61!
![Page 62: COMP 110-001 Mid-Term Reviewcobweb.cs.uga.edu/~yihong/COMP110/files/Slides/Lecture13_May27.pdfClass ! A class is the definition of a kind of object • A blueprint for constructing](https://reader030.vdocuments.us/reader030/viewer/2022041103/5f0353487e708231d408a7fe/html5/thumbnails/62.jpg)
Next Class § Go through questions from mid-term
practice worksheet § Q&A