Table of Contents
Introduction to Java
- Slides: Download
Software
- Please install the Java Development Kit first: Download
- We will use professional IDE for Java programming called IntelliJ - please install it. It is available on Linux, Windows and Mac (download the Community Version): IntelliJ Idea IDE
Exercises
Hello World program
Variables
public class Main{ public static void main(String [] argv){ String name = "Szymon"; double a = 5; double b = 6; System.out.println("Hello "+name+". I know that "+ a+"+"+b+"="+(a+b)); } }
Try other mathematical operations. What about power? Try different types: int, double, float, boolean?
Arrays
public class Main{ public static void main(String [] argv){ String name = "Szymon"; double [] arrayOfNumbers = new double[4]; arrayOfNumbers[0] = 6; System.out.println("Hello "+name+ ". The first element in the array " + " is "+arrayOfNumbers[0]); } }
Try print out other elements form the array.
If statements
public class Main{ public static void main(String [] argv){ String name = "Szymon"; double [] arrayOfNumbers = new double[4]; arrayOfNumbers[0] = 6; if(arrayOfNumbers[0] == 6){ System.out.println("Yes, it is 6"); }else if(arrayOfNumbers[0] != 6){ System.out.println("No it is not 6"); } } }
Try other if statements.
Input
public class Main{ public static void main(String [] argv){ String name; int age; System.out.println("What is your name?"); Scanner scanner = new Scanner(System.in); name = scanner.nextLine(); System.out.println("What is your age?"); age = scanner.nextInt(); } }
Try to add statement that will ask for the value of pi and print all the information out.
Input and If statements
public class Main{ public static void main(String [] argv){ int age; Scanner scanner = new Scanner(System.in); System.out.println("What is your age?"); age = scanner.nextInt(); if(age < 18){ System.out.println("Access denied") }else{ System.out.println("Access granted") } } }
Try more if statements. (you are old, wrong number); Read password from the user and test if it is OK. This one is tricky.
Loops I
public class Main{ public static void main(String [] argv){ int age; Scanner scanner = new Scanner(System.in); while(true){ System.out.println("What is your age?"); age = scanner.nextInt(); if(age < 18){ System.out.println("Access denied") }else{ System.out.println("Access granted") } } } }
Try to condition the loop on the value of age.
Loops II
public class Main{ public static void main(String [] argv){ double numbers [] = new double[5]; Scanner scanner = new Scanner(System.in); for(int i = 0; i < numbers.length; i++){ numbers[i] = scanner.nextInt(); } } }
Use loop to print out the number that you read; Create a program that will print out multiplication table of given size;
Functions
public class Main{ public static void main(String [] argv){ double numbers [] = new double[5]; double numbers2D [][] = new double[5][3]; Scanner scanner = new Scanner(System.in); for(int i = 0; i < numbers.length; i++){ numbers[i] = scanner.nextInt(); } printOutTheArray(numbers); } public static void printOutTheArray(double [] array){ for(int i = 0; i < array.length; i++){ System.out.println(array[i]); } } }
Add statement that will prompt the user for the next number. Use loop to print out the number that you read; Create a program that will print out multiplication table of given size;
The riddle
Write a program that solves the riddle. But now, assume that the sum of processors is 72.
Solution
<hidden Click here if you want to see the possible solution>
- Main.java
public class Main{ public static void main(String [] argv){ int product= 36; int sums [][] = new int[product][4]; int sumIndex = 0; System.out.println("----------All possibilities --------"); for(int first = 1; first <= product; first++ ){ for(int second = first; second <=product/first; second++){ for(int third = second; third <= product/(first*second); third++){ if((first*second*third) == product) { System.out.println("|" + first + "|" + "|" + second + "|" + "|" + third + "||" + (first * second * third) + " -> " + (first + second + third)); sums[sumIndex][0] = first; sums[sumIndex][1] = second; sums[sumIndex][2] = third; sums[sumIndex][3] = first+second+third; sumIndex++; } } } } System.out.println("----------Ambiguous solutions--------"); int [][] ambs = findAmbiguous(sums); for(int i = 0; i < ambs.length && ambs[i][0] != 0;i++){ System.out.println("|" + ambs[i][0] + "|" + "|" + ambs[i][1] + "|" + "|" + ambs[i][2] + "||" + (ambs[i][0]+ambs[i][1]+ambs[i][2])); } System.out.println("----------Solution--------"); int [] solution = findMostPowerful(ambs); if(solution != null) { System.out.println("The solution is First: " + solution[0] + " second: " + solution[1] + " and third: " + solution[2]); } } public static int [][] findAmbiguous(int [][] sums){ int [][] ambiguous = new int[sums.length][3]; int ambIndex = 0; for(int i = 0; i < sums.length && sums[i][3] != 0;i++){ for(int j = i+1; j < sums.length && sums[j][3] != 0;j++){ if(sums[i][3] == sums[j][3]){ ambiguous[ambIndex][0] = sums[i][0]; ambiguous[ambIndex][1] = sums[i][1]; ambiguous[ambIndex][2] = sums[i][2]; ambIndex++; ambiguous[ambIndex][0] = sums[j][0]; ambiguous[ambIndex][1] = sums[j][1]; ambiguous[ambIndex][2] = sums[j][2]; ambIndex++; } } } return ambiguous; } public static int [] findMostPowerful(int [][] ambiguous){ for(int i = 0;i < ambiguous.length;i++){ if(ambiguous[i][0] > ambiguous[i][1] && ambiguous[i][0] > ambiguous[i][2]){ return ambiguous[i]; }else if(ambiguous[i][1] > ambiguous[i][0] && ambiguous[i][1] > ambiguous[i][2]){ return ambiguous[i]; }else if(ambiguous[i][2] > ambiguous[i][0] && ambiguous[i][2] > ambiguous[i][1]){ return ambiguous[i]; } } return null; } }
</hidden>