Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
1.1 Programs, Fields
First Example
1
2
Second Example
1
2
3
4
5
6
7
8
class SecondExample {
int theNumberFive = 5;
int theNumberEight = 8;
int theNumberTwo = 2;
int x = 58;
int y = 73;
}
1.2 Java as a Calculator
Operators
1
2
3
4
class Operators {
int theAnswer = 5 + 2 * 45;
}
Operators
1
2
3
4
5
class Operators {
int theAnswer = (5 + 2) * 45;
}
Not Quite Math
1
2
3
4
class NotQuiteMath {
int theAnswer1 = (5 + 3) / 2;
}
Not Quite Math
1
2
3
4
class NotQuiteMath {
int theAnswer2 = (5 + 4) / 2;
}
Negative Results
1
2
3
4
class NegativeResults {
}
1.3 Syntax
Syntax
1
2
3
4
class Syntax
int theNumberFive = 5;
}
Syntax
1
2
3
4
class Syntax
int theNumberFive = 5;
}
Syntax
1
2
3
4
class Syntax {
int theNumberFive = 5
}
2.1 Using Fields
Using Fields
1
2
3
4
5
6
class UsingFields {
int distAfter2sec = (10 / 2) * (2 * 2);
int distAfter4sec = (10 / 2) * (4 * 4);
int distAfter6sec = (10 / 2) * (6 * 6);
}
Using Fields
1
2
3
4
5
6
7
class UsingFields {
int gravity = 10;
int distAfter2sec = (this.gravity / 2) * (2 * 2);
int distAfter4sec = (this.gravity / 2) * (4 * 4);
int distAfter6sec = (this.gravity / 2) * (6 * 6);
}
Getting Paid
1
2
3
4
5
6
class Pay {
int hourlyRate = 20;
int numHours = 15;
int pay = this.hourlyRate * this.numHours;
}
Calculating Revenue
1
2
3
4
5
6
7
8
class Revenue {
int costPerItem = 5;
int numSoldPerWeek = 20;
int revenuePerWeek = this.costPerItem * this.numSoldPerWeek;
int weeks = 10;
int totalRevenue = this.revenuePerWeek * this.weeks;
}
2.2 Beyond ints
String Examples
1
2
3
4
5
6
class StringExamples {
String name = "Dorothy Vaughan";
String password = "s00peR_C_kret";
String sentence = "But Java, and programming languages in general, support many other kinds of data.";
}
Conctenating Strings
1
2
3
4
5
6
class Name {
String firstName = "Ada";
String lastName = "Lovelace";
String fullName = this.firstName + this.lastName;
}
Conctenating Strings
1
2
3
4
5
6
class Name {
String firstName = "Ada";
String lastName = "Lovelace";
String fullName = this.firstName + " " + this.lastName;
}
Video Game Example Initial Attempt
1
2
3
4
5
6
7
8
class VideoGame {
String mainTitle = "Final Fantasy XV";
String mainPrice = "$49.99";
String dlcTitle = "Episode Ardyn";
String dlcPrice = "$4.99";
String totalPrice = mainPrice + dlcPrice;
}
Video Game Example Second Attempt
1
2
3
4
5
6
7
8
class VideoGame {
String mainTitle = "Final Fantasy XV";
double mainPrice = 49.99;
String dlcTitle = "Episode Ardyn";
double dlcPrice = 4.99;
double totalPrice = mainPrice + dlcPrice;
}
Number Precision
1
2
3
4
5
6
7
class Numbers {
int num1 = 1 + 2;
int num2 = 3;
double num3 = 1.1 + 2.2;
double num4 = 3.3;
}
Rectangle
1
2
3
4
5
6
7
class Rectangle {
double length = 4.2;
double width = 1.5;
double perimeter = (2 * length) + (2 * width);
double area = length * width;
}
2.3 Java Checks Types
Mixing Strings and ints
1
2
3
4
class JavaChecksTypes {
int firstName = "Dorothy";
}
Mixing Strings and ints
1
2
3
4
class JavaChecksTypes {
String firstName = 10;
}
Mixing Strings and ints
1
2
3
4
class JavaChecksTypes {
int answer = 5 + (10 / "a");
}
3.1 Capturing Repeated Work with Methods
Using Methods
1
2
3
4
5
6
7
class Distance {
int gravity = 10;
int distAfter2sec = (this.gravity / 2) * (2 * 2);
int distAfter4sec = (this.gravity / 2) * (4 * 4);
int distAfter6sec = (this.gravity / 2) * (6 * 6);
}
Using Methods
1
2
3
4
5
6
7
8
9
10
11
12
class Distance2 {
int gravity = 10;
int distanceAfter(int seconds) {
return (this.gravity / 2) * (seconds * seconds);
}
int distAfter2sec = this.distanceAfter(2);
int distAfter4sec = this.distanceAfter(4);
int distAfter6sec = this.distanceAfter(6);
}
Using Methods
1
2
3
4
5
6
7
8
9
10
11
12
class Distance {
int gravity = 10;
int distanceAfter(int seconds) {
return (this.gravity / 2) * (seconds * seconds);
}
int distAfter2sec = this.distanceAfter(2);
int distAfter4sec = this.distanceAfter(4);
int distAfter6sec = this.distanceAfter(6);
}
3.2 Methods as Descriptions of Tasks
Implementing Tasks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class MadLibs {
/*
Places the given adjective and number into the sentence template to
create a full sentence.
*/
String fillIn(String adjective, int number) {
return "The professor's explanation was " + adjective +
", probably because he's been teaching for " + number + " years.";
}
String example1 = this.fillIn("useless", 2);
String example2 = this.fillIn("relevant", 3);
String example3 = this.fillIn("wise", 22);
}
Following the Design Recipe
1
2
3
4
5
6
7
8
9
10
11
class WeeklyPay {
/*
__STEP2__
*/
__STEP1__ {
__STEP4__;
}
__STEP3__;
}
Following the Design Recipe
1
2
3
4
5
6
7
8
9
10
11
class Multiplication {
/*
__STEP2__
*/
__STEP1__ {
__STEP4__;
}
__STEP3__;
}
3.3 With Great Power Comes More Opportunity for Mistakes
Understanding Errors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MadLibs {
/*
Places the given adjective and number into the sentence template to
create a full sentence.
*/
String fillIn(String adjective, int number) {
return "The professor's explanation was " + adjective +
", probably because he's been teaching for " + number + " years.";
}
// Instead of String example1 = this.fillIn("useless", 2); try this:
String example1 = this.fillIn("useless");
}
Understanding Errors
1
2
3
4
5
6
7
8
9
10
11
12
13
class MadLibs {
/*
Places the given adjective and number into the sentence template to
create a full sentence.
*/
String fillIn(String adjective, int number) {
return "The professor's explanation was " + adjective +
", probably because he's been teaching for " + number + " years.";
}
String example1 = this.fillIn("useless", 2);
}
Understanding Errors
1
2
3
4
5
6
7
8
9
10
11
12
class MadLibs {
/*
Places the given adjective and number into the sentence template to
create a full sentence.
*/
// NOTE: String has been changed to int in the return type below.
int fillIn(String adjective, int number) {
return "The professor's explanation was " + adjective +
", probably because he's been teaching for " + number + " years.";
}
}
Understanding Errors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MadLibs {
/*
Places the given adjective and number into the sentence template to
create a full sentence.
*/
String fillIn(String adjective, int number) {
return "The professor's explanation was " + adjective +
", probably because he's been teaching for " + number + " years.";
}
// Instead of String example1 = this.fillIn("useless", 2); try this:
String example1 = this.fillIn(1, 2);
}
4.1 Beyond Arithmetic and Concatenation
Using Conditions
1
2
3
4
5
class Comparisons {
boolean fourIsLessThanFive = 4 < 5;
boolean fiveIsLessThanFour = 5 < 4;
}
Using Conditions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class WeeklyPay {
// Calculate weekly pay at the given rate and number of hours worked.
// If hours is above 40, pay at double the rate for hours beyond 40. Pay at
// rate for the first 40 hours. If the number of hours is less than 40, don't
// add any overtime.
int weekly(int hours, int rate) {
return 0;
}
int exactly40 = this.weekly(40, 10); // Should be 400
int someOvertime = this.weekly(45, 10); // Should be 400 + 100, total 500
int lessThan40 = this.weekly(30, 20); // Should be 600
}
Using Conditions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class WeeklyPay {
// Calculate weekly pay at the given rate and number of hours worked.
// If hours is above 40, pay at double the rate for hours beyond 40. Pay at
// rate for the first 40 hours. If the number of hours is not more than 40,
// don't add any overtime.
int weekly(int hours, int rate) {
if(hours > 40) {
return (40 * rate) + ((hours - 40) * (rate * 2));
}
else {
return hours * rate;
}
}
int exactly40 = this.weekly(40, 10); // Should be 400
int someOvertime = this.weekly(45, 10); // Should be 400 + 100, total 500
int lessThan40 = this.weekly(30, 25); // Should be 750
int matchTest1 = this.weekly(20, 35);
int matchTest2 = this.weekly(0, 50);
int matchTest3 = this.weekly(43, 24);
}
Using Conditions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class WeeklyPay {
// Calculate weekly pay at the given rate and number of hours worked.
// If hours is above 40, pay at double the rate for hours beyond 40. Pay at
// rate for the first 40 hours. If the number of hours is less than 40, don't
// add any overtime.
int weekly(int hours, int rate) {
if(hours > 40) {
return 40 * rate + ((hours - 40) * (rate * 2));
}
else {
return hours * rate;
}
}
int exactly40 = this.weekly(40, 10); // Should be 400
int someOvertime = this.weekly(45, 10); // Should be 400 + 100, total 500
int lessThan40 = this.weekly(30, 25); // Should be 750
}
4.2 More Examples of Decision-Making Programs
Using Conditions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Grades {
String gradeForNumber(int score) {
if(score >= 90) {
return "A";
}
else if(score >= 80) {
return "B";
}
else if(score >= 70) {
return "C";
}
else if(score >= 60) {
return "D";
}
else if(score < 60) {
return "F";
}
}
}
Using Conditions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Grades {
String gradeForNumber(int score) {
if(score >= 90) {
return "A";
}
else if(score >= 80) {
return "B";
}
else if(score >= 70) {
return "C";
}
else if(score >= 60) {
return "D";
}
else {
return "F";
}
}
String example1 = gradeForNumber(84);
String example2 = gradeForNumber(70);
String example3 = gradeForNumber(48);
}
Using Conditions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Grades {
String gradeForNumber(int score) {
if(score >= 90) {
return "A";
}
else if(score >= 80) {
return "B";
}
else if(score >= 70) {
return "C";
}
else if(score >= 60) {
return "D";
}
else {
return "F";
}
}
String example1 = gradeForNumber(84);
String example2 = gradeForNumber(70);
String example3 = gradeForNumber(48);
}
5.1 Representing Compound Data
Combining Data into Classes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Student {
String studentID;
boolean inState;
int birthYear;
Student(String studentID, boolean inState, int birthYear) {
this.studentID = studentID;
this.inState = inState;
this.birthYear = birthYear;
}
}
class ExamplesStudent {
Student s1 = new Student("A12345678", true, 1996);
Student s2 = new Student("A98765432", false, 1993);
Student s3 = new Student("A18273645", true, 1999);
}
5.2 More on Methods
Creating Instances of Classes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Student {
String studentID;
boolean inState;
int birthYear;
Student(String studentID, boolean inState, int birthYear) {
this.studentID = studentID;
this.inState = inState;
this.birthYear = birthYear;
}
/*
__STEP2__
*/
__STEP1__ {
__STEP4__;
}
/*
__STEP2__
*/
__STEP1__ {
__STEP4__;
}
}
class ExamplesStudent {
__STEP3__;
__STEP3__;
}
5.3 Variables and Scope
Total Cost Calculation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Receipt {
String purchaseMessage(String customerName, int cost, double tipPercent) {
return "Bill for " + customerName + ": Cost $" + cost + ", Total $" + (_______________) + ", tip $" + (cost * tipPercent);
}
// "Bill for Greg: Cost $5, Total $6, Tip $1"
String tipExample1 = this.purchaseMessage("Greg", 5, 0.2);
// "Bill for Joe: Cost $4, Total $4.40, Tip $0.40"
String tipExample2 = this.purchaseMessage("Joe", 4, 0.1);
// NOTE: expectations don't include the number of decimal places Java
// decides to report for double! We would need to round/specify 2 decimal
// places to have nicer-looking output. That's possible, but not shown here.
}
Variable fill-in
1
2
3
4
5
6
7
class Receipt {
String purchaseMessage(String customerName, int cost, double tipPercent) {
double tip = cost * tipPercent;
return "Bill for " + customerName + ": Cost $" + cost + ", Total $" + __________ + ", Tip $" + __________;
}
}
Bad Scope
1
2
3
4
5
6
7
8
9
10
class Receipt {
String purchaseMessage(String customerName, int cost, double tipPercent) {
double tip = cost * tipPercent;
return "Bill for " + customerName + ": Cost $" + cost + ", Total $" + (cost + tip) + ", Tip $" + tip;
}
String example1 = this.purchaseMessage("Joe", 10, 0.1);
double example1Tip = tip;
}
Bad Scope Experimentation