In your Documents, make a file named java_exercises.java
cd Documents
touch Java_exercises.java
nano Java_exercises.java
class Java_exercises {
public static void main(String[] args) {
// exercise code here
}
}
In Java, // denotes comments. Everything after the // is commented out, so the computer doesn't register it
The value on the right gets assigned to the variable on the left
ex int num = 10;
ex num == 10;
class Java_exercises {
public static void main(String[] args) {
int irene = 100;
System.out.println(irene);
}
}
class Java_exercises {
public static void main(String[] args) {
int irene = 100;
irene = 200;
int john;
john = irene;
System.out.println(john)
}
}
String fred = "This is a string.";
System.out.println(fred);
fred = "This is not a string;
int age = 5;
fred = "My age is " + age;
System.out.println(fred);
float num = 5*5+10/3;
System.out.println(num);
num = num*12;
System.out.println(num);
String shopping_list = "eggs, milk, baking soda";
String[] shopping_list = new String[5];
shopping_list[0] = "eggs";
shopping_list[1] = "milk";
shopping_list[2] = "baking soda";
dataType[] arrayName = new dataType[size];
if(age > 17) {
System.out.print("You are too old!");
}
int age = 18;
if (age == 12) {
System.out.print("Hello");
} else {
System.out.print("Goodbye");
}
int age = 12;
if (age == 11) {
System.out.print("You are 11");
} else if (age == 12) {
System.out.print("You are 12");
} else if (age < 13) {
System.out.print("You are younger than 13");
} else {
System.out.print("You are older than 13");
}
What will be printed?
if (age == 12 || age == 13 || age == 14) { System.out.print("You are "+age+" yrs old); }
if (age >= 10 && age <= 14) { System.out.print("You are "+age+" yrs old); }
Create an array containing at least 3 of your hobbies. Then, use conditional statements to print each element if they are less than 10 characters, or a string saying "That information is inaccessible, there are X characters." where X is the amount of characters in the string.
Hint: append .length() to the String name (Ex. hobby1.length()) to access an int containing the length of the string.
for (type var = value; var == condition; var = var + 1) {}
int y = 1;
for (int x = 0; x < 5; x++) {
y = y + x;
System.out.println(y);
}
while (condition) { // code here }
int x = 1;
while (x < 200) {
System.out.println(x);
x = x*2;
}
while (true) {
// tons of code
if someCondition == true {
break;
}
}
String[] array = new String[3];
array[0] = "a";
array[1] = "b";
array[2] = "c";
for (int u = 0; u < array.length; u++) {
System.out.println(array[u]);
for (int v = 0; v < array.length; v++) {
System.out.println(array[v]);
}
}
If you have $100 saved in your bank account, and they pay you 3% interest every year, how much money will you have for each year, up to 10 years?
Hint: Use a for loop (you can use a while loop, but it'll take more code).
modifier returnType function(parameter) {
// code goes here
}
You define (make) the method outside of main() like this:
public static void printName(String name) {
System.out.println("Hello " + name);
}
And call the method inside of main() like this:
printName("John");
Which will print out: Hello John
Another example, except with return instead of print():
public static int savings(int job, int spending) {
return job - spending;
}
And call it (inside of main()) like this:
int money = savings(20, 5);
System.out.println(money);
What does return do? What's the difference between return and print()?
// This is a global variable
int a = 0;
if (a == 0) {
// This is still a global variable
int b = 1;
}
public static void my_method(int c) {
// this is a local variable
int d = 3;
System.out.println(c);
System.out.println(d);
}
// Now we call the function, passing the value 7
// as the first and only parameter
my_function(7);
// a and b still exist
System.out.println(a);
System.out.println(b);
// c and d don't exist anymore -- these statements
// will give us name errors!
System.out.println(c);
System.out.println(d);
public static void main(String[] args) {
// code that gets executed
}
// This is how your code should be structured!
public class MyClass {
public static void method1() {
// code
}
public static double multiply(int num1, int num2) {
// code
}
public static void main(String[] args) {
method1();
double product = multiply(12, 3.3334);
}
}
Define a method overlapping() that takes two arrays as arguments (parameters) and returns true if they have at least one element in common, false otherwise.
Then call the method, and if it evaluates to true, print out a string, "There is a common element in the arrays!" Print "These arrays do NOT have a common element" if it is false.
Note: Write this using a nested for-loop
If you finish early: Modify the code so the print statement specifies the amount of common elements.
// This class models a bicycle
public class Bicycle {
// Bicycle has two member variables
public int gear;
public int speed;
// Bicycle has one constructor
public Bicycle(int startSpeed, int startGear) {
gear = startGear;
speed = startSpeed;
}
// Bicycle has three methods
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
ClassType objectName = new ClassType(constructorParameters);
Ex. Bicycle bike = new Bicycle(45, 1);
objectName.method(parameter);
Ex. bike.speedUp(3);