Revised Potato Pirates x Java (3-Hour Resource)
Revised Potato Pirates x Java (3-Hour Resource)
Website: www.potatopirates.game
Email: [email protected], [email protected]
1. Getting Started
a. What is Java? 3
b. What is a programming language? 3
c. Downloading & installing Eclipse 4
d. My first Java program 5
2. Chapter 1: Variables
a. What are variables? 8
b. Get the value of a variable 9
c. Comments 9
d. Updating the value of a variable 10
e. Why do we use variables? 10
f. Mathematical operators 11
g. Strings 12
h. String concatenation 13
i. String indexing 14
j. Data types 15
k. Type casting 15
l. Exercise 16
3. Chapter 2: For-Loop
a. What is a for loop? 18
b. Breakdown (for loop) 19
c. Nested for loop 20
d. Breakdown (nested for loop) 21
e. Exercise 22
7. Answer Sheet 38
1. Go to https://www.eclipse.org/downloads/
2. Download Eclipse Installer (32 or 64 bit).
Install
Opening Eclipse
Windows
1. Go to Start button.
2. Type “Eclipse” in the “Search programs and files textbox”.
Macintosh OS
1. Press Cmd + space
2. Type “Eclipse”
1. 2.
3. 4.
5.
3.
What’s happening?
When you write System.out.println(“Ahoy, Matey!”);
, you are telling the system to print out the line “Ahoy, Matey!”.
Try changing “Ahoy, Matey!” to other texts. See what happens.
Variable name
Every variable is identified with a name. This name can be anything you
want as long as it’s not a keyword.
More on keywords in the cheat sheet!
© Copyright Codomo Pte Ltd 2022 8
Variables
Getting the value of a variable
After storing a value in a variable we can retrieve it by “calling” its
name. The code below demonstrates how to retrieve a value stored
in a variable.
Java Code:
public static void main(String[] args) {
int crew = 10; // Set variable “crew” as 10
System.out.println(crew); // print the value of “crew”
}
Console:
Comments ( // symbol )
A comment is a programmer-readable explanation which is usually
used to explain the meaning of certain lines of code. Words
appearing after // will be ignored by the computer. Very useful!
BODMAS
Order: ** Multiplication: * Subtraction: -
Oh, and don’t forget to use String as your data type when
initialising it! Try running the code below on Eclipse:
Java Code:
public static void main(String[] args) {
String control1 = "for 3 times, ";
String action1 = "roast";
System.out.println(control1);
System.out.println(action1);
}
Console:
Java Code:
public static void main(String[] args) {
String control1 = "for 3 times, ";
String action1 = "roast";
String final_words = control1 + action1;
System.out.println(final_words);
}
Console:
Let’s extract the letter ‘A’. Below is the Java code to extract it.
Java Code:
public static void main(String[] args) {
String bar = "POTATO";
char foo = bar.charAt(3);
System.out.println(foo);
}
‘char’ refers to ‘character’ data type
Returns the character at index 3
Console:
Type casting
Java includes methods to convert from one data type to another.
Run the code below to convert between string & integers.
Converting Integer to String
public static void main(String[] args) {
int x = 3;
String x_str = Integer.toString(x);
System.out.println(x_str+"potatoes");
}
Convert x to string
Concatenate 2 strings
1. Initialise a variable “x” with value 10. Then, add 5 to it. The
final value of x should print 15.
2. Initialise a variable “a” with value 10. Then, divide it by 2. The
final value of “a” should print 5.
3. Initialise a variable “b” with value 12. Then, divide it by 3.
Find the remainder.
Data Types
Type this out in the Eclipse IDE and see what it does.
When you’re done, let’s take a closer look at each part of the code.
Below shows the process of how a for loop will work. On the first
iteration, the variable “i” will carry the value of 1. On the next
iteration, it will increase by 1. The loop continues until the
condition is no longer met and becomes false.
Iteration Value of i
Did you know?
st
1 1 In Java, i = i + 1 can
be written as i++
2nd 2
3rd 3
Nested/
In Potato Pirates Outer loop In Java Inner loop
Loop-ception.
What a beauty!
Outer Loop
for (int j=0;j<3;j++) {
for (int i=0;i<3;i++) {
System.out.println("Fry");
}
}
Inner Loop
for (int j=0;j<3;j++) {
for (int i=0;i<3;i++) {
System.out.println("Fry");
}
}
Code:
for (int i=5; i<____; i+=5){
enemy_crew = enemy_crew - 1;
}
Type this out in the Eclipse IDE and see what it does. Remember
to declare the variable “crew” before this code. When you’re done,
let’s take a closer look at each part of the control statement.
Note: Hit the red button above the output to stop the infinite loop!
The example below shows how a while loop will work. The crew
begins with 7 crew members. On the first iteration, the while loop
will check if there are more than 4 crew members. Since 7 > 4 is
true, it will execute the action to reduce the crew by 2. On the
next iteration, the action will run again as 5 > 4. Finally, it stops at
the third iteration as 3 > 4 is false.
int crew = 7;
while (crew > 4) {
crew = crew - 2;
}
System.out.println(crew);
3rd 3 FALSE NA
Logical Operators
Logical operators are typically used with boolean values. When
they are, they return a boolean value. Below are some examples.
Java Code:
public static void main(String[] args) {
int crew1 = 3;
int crew2 = 5;
boolean isDead = false;
System.out.println(crew1 == 3);
System.out.println(crew2 <= 2);
System.out.println(crew1 > crew2);
System.out.println(crew1>crew2 && crew2 ==5);
System.out.println(crew1>crew2 || crew2 ==5);
System.out.println(!isDead);
}
Console:
This is the
enemy
A B C
3) Which card deck can deal the highest damage to the enemy?
Convert all 3 decks into Java code.
if (crew <= 4) {
System.out.println(“Fry”);
}
else {
System.out.println(“Mash”);
}
Type the code out in Eclipse and see what it does. Remember to
declare the variable “crew” first. When you’re done, we’ll take a
closer look at the if statement.
if (crew <= 4)
if (crew <= 4) {
// code A
}
else {
// code B
}
Legend
Condition: A boolean expression. It will return True or False
In Potato Pirates
Draw 3 cards
In Java
int crew = 2;
switch (crew) {
case 1:
System.out.println("get a new ship and a potato");
break;
case 2:
System.out.println("pick a card from the discard pile");
break;
case 3:
System.out.println("draw 3 cards");
break;
default:
System.out.println("you get nothing");
}
switch(crew)
General Structure
switch (crew) {
case 1:
// code A
break;
case 2:
// code B
break;
…
Default:
// code C
}
switch(ships){ Switch
Java Syntax Cheat Sheet
statement
case 1:
While Loop Syntax If-else Loop Syntax
case 2: Condition
Actions
} if (crew > 4) {
Action
} }
Actions
else{
For Loop Syntax Initialising Variables }
Increment/
Initialisation Decrement
Data Type: Value:
Data Types
for (int i=1;i<=3;i++){ int crew = 10;
} String → String
Action Variable name: Integer → int
Condition Character → char
Boolean → boolean
Java Keywords
Integer → String Integer.toString()
+ - * Or ||
Division Remainder
Not !
Arithmetic
/ % Operators
37
Answer Sheet for Exercises
Variables
1. 2. 3.
int x = 10; int a = 10; int b = 12;
x = x + 5; a = a / 2; b = b % 3;
System.out.println(x); System.out.println(a); System.out.println(b);
1. 2.
String a = "Hello "; String word1 = "POTATO PIRATES";
String b = "Potato King"; char s_letter = word1.charAt(13);
System.out.println(a+b); System.out.println(s_letter);
Data Types
1.
String x = "10";
int x_int = Integer.valueOf(x);
System.out.println(x_int + 20);
2.
int y = 7;
String y_str = Integer.toString(y);
System.out.println(y_str + " Potato Kings");
Basic Questions
1. 2.
for (int i=1; i<101; i++) { for (int i=3; i<21; i++) {
System.out.println(i); System.out.println(i);
} }
1.
int enemy_crew = 10;
for (int i=0; i<2; i++) {
enemy_crew = enemy_crew - 1;
}
System.out.println(enemy_crew);
2.
int y = 2;
int enemy_crew = 30;
for (int j=0; j<3; j++) {
for (int i=0; i<y; i++) {
enemy_crew = enemy_crew - 3;
}
}
System.out.println(enemy_crew);
3.
int enemy_crew = 10;
int enemy_left = 4;
int enemy_killed = enemy_crew-enemy_left;
int mash = 2;
int cards = enemy_killed/mash;
System.out.println(cards); //returns 3
Basic Questions
1. 2.
int num = 10; int num = 10;
while (num >= 1) { while (num >= 5) {
System.out.println(num); System.out.println(num);
num = num - 1; num = num - 1;
} }
3a. 3b.
int crew = 16; int crew = 16;
while (crew > 5) { for (int i=0;i<3;i++){
crew = crew - 3; crew = crew - 3;
} }
System.out.println(crew); System.out.println(crew);
2.
int enemy_crew = 10;
if (enemy_crew <= 5) {
for (int i=0; i<2; i++) {
enemy_crew = enemy_crew - 3;
}
}
else{
while (enemy_crew > 6) {
enemy_crew = enemy_crew - 2;
}
}
System.out.println(enemy_crew);