├── week_1 ├── 001 Name.java ├── 002 Greeter.java ├── 006 Addition.java ├── 000 Application.java ├── 003 Spruce.java ├── 007 Multiplication.java ├── 019 AgeCheck.java ├── 010 Circumference.java ├── 014 PositiveValue.java ├── 005 SecondsOfTheYear.java ├── 016 EvenOrOdd.java ├── 015 AgeOfMajority.java ├── 009 Divider.java ├── 011 BiggerNumber.java ├── 022 Password.java ├── 021 LeapYear.java ├── 008 Adder.java ├── 004 Variables.java ├── 020 Usernames.java ├── 017 GreaterNumber.java ├── 023 Temperatures.java ├── 012 SumOfTheAges.java ├── 018 GradesAndPoints.java ├── 013 Main.java ├── 023 Graph.java └── 024 NhlStatisticsPart2.java ├── week_6 ├── 105 Main.java ├── 100 Main.java ├── 097 Main.java ├── 096 Main.java ├── 102 Main.java ├── 094 Main.java ├── 099 Main.java ├── 101 StringUtils.java ├── 106 Main.java ├── 095 Main.java ├── 101 Main.java ├── 094 Person.java ├── 103 Bird.java ├── 101 Book.java ├── 106 BinarySearch.java ├── 098 Main.java ├── 094 Phonebook.java ├── 103 Main.java ├── 095 Money.java ├── 100 NightSky.java ├── 101 Library.java ├── 105 GuessingGame.java ├── 104 Main.java ├── 102 Grade.java └── 103 Commands.java ├── week_4 ├── 073 Main.java ├── 080 Main.java ├── 080 Dice.java ├── 082 Main.java ├── 081 Program.java ├── 075 Main.java ├── 072 Accounts.java ├── 074 Multiplier.java ├── 076 Menu.java ├── 072 Account.java ├── 074 Main.java ├── 073 Product.java ├── 081 PasswordRandomizer.java ├── 076 Main.java ├── 071 SmartCombining.java ├── 075 DecreasingCounter.java ├── 079 NumberStatistics.java ├── 070 CombiningArrayLists.java ├── 082 LotteryNumbers.java ├── 077 LyyraCard.java ├── 078 BoundedCounter.java ├── 079 Main.java ├── 077 Main.java ├── 078 Main.java └── 083 HangmanFigure.java ├── README ├── week_5 ├── 083 Main.java ├── 093 Main.java ├── 089 Main.java ├── 084 Main.java ├── 085 Reformatory.java ├── 090 Main.java ├── 091 Main.java ├── 083 TestProgram.java ├── 086 Main.java ├── 083 HirsipuuKeyAdapter.java ├── 083 HangmanUserInterface.java ├── 088 Student.java ├── 086 LyyraCard.java ├── 085 Main.java ├── 090 Player.java ├── 092 Main.java ├── 087 Main.java ├── 089 BoundedCounter.java ├── 088 Main.java ├── 087 Apartment.java ├── 089 Clock.java ├── 085 Person.java ├── 090 Team.java ├── 093 Person.java ├── 091 MyDate.java ├── 083 HangmanLogic.java ├── 092 MyDate.java ├── 093 MyDate.java ├── 084 Counter.java └── 086 CashRegister.java ├── week_2 ├── 027 FromOneToHundred.java ├── 037 PrintingOutText.java ├── 028 FromHundredToOne.java ├── 029 EvenNumbers.java ├── 030 UpToCertainNumber.java ├── 032 TheSumOfSetOfNumbers.java ├── 035 SumOfThePowers.java ├── 026 SumOfManyNumbers.java ├── 033 TheSumBetweenTwoNumbers.java ├── 031 LowerLimitAndUpperLimit.java ├── 034 Factorial.java ├── 038 ManyPrints.java ├── 025 SumOfThreeNumbers.java ├── 041 GuessingNumberGame.java ├── 036 LoopsEndingRemembering.java ├── 040 PrintingLikeBoss.java ├── 042 HangmanUserInteface.java └── 039 Printing.java ├── .gitignore └── week_3 ├── 045 Greatest.java ├── 043 SumOfNumbers.java ├── 052 ReversingName.java ├── 053 FirstPart.java ├── 048 FirstCharacter.java ├── 051 SeparatingCharacters.java ├── 054 TheEndPart.java ├── 049 LastCharacter.java ├── 047 LengthOfName.java ├── 044 Least.java ├── 046 AverageOfGivenNumbers.java ├── 057 Words.java ├── 061 NumberOfItems.java ├── 050 FirstCharacters.java ├── 058 RecurringWord.java ├── 063 SumOfNumbers.java ├── 066 TheGreatest.java ├── 056 ReversingText.java ├── 055 WordInsideWord.java ├── 059 WordsInReverseOrder.java ├── 065 LengthsOfStrings.java ├── 060 WordsInAlphabeticalOrder.java ├── 064 AverageOfNumbers.java ├── 062 RemoveLast.java ├── 069 Palindromi.java ├── 068 MoreThanOnce.java └── 067 Variance.java /week_1/001 Name.java: -------------------------------------------------------------------------------- 1 | public class Name { 2 | 3 | public static void main(String[] args) { 4 | System.out.print("Andrew Rowlinson");// Print my name 5 | } 6 | 7 | } -------------------------------------------------------------------------------- /week_6/105 Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | public static void main(String[] args) { 4 | // test your program here 5 | GuessingGame game = new GuessingGame(); 6 | game.play(1,10); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /week_1/002 Greeter.java: -------------------------------------------------------------------------------- 1 | public class Greeter { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("Hello world!"); 5 | System.out.println("(And all the people of the world)"); //Print with line break 6 | } 7 | 8 | } -------------------------------------------------------------------------------- /week_4/073 Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | // You can test your new class here, try e.g.: 4 | 5 | Product t = new Product("Banana", 1.1, 13); 6 | t.printProduct(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | #Code from University of Helsinki's Java programming course 2 | These are exercises from Helsinki University's online course: Object-Oriented programming with Java, part I 3 | The materials for this course are available at: http://mooc.fi/courses/general/programming/ -------------------------------------------------------------------------------- /week_5/083 Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | HangmanLogic logic = new HangmanLogic("parameter"); 5 | HangmanUserInterface game = new HangmanUserInterface(logic); 6 | game.start(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /week_5/093 Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Person pekka = new Person("Pekka", new MyDate(15, 2, 1983)); 4 | Person steve = new Person("Steve"); 5 | 6 | System.out.println( pekka ); 7 | System.out.println( steve ); 8 | } 9 | } -------------------------------------------------------------------------------- /week_2/027 FromOneToHundred.java: -------------------------------------------------------------------------------- 1 | 2 | public class FromOneToHundred { 3 | 4 | public static void main(String[] args) { 5 | int number=0; 6 | while (number<=99) { 7 | number++; 8 | System.out.println(number);} 9 | // Write your program here 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /week_2/037 PrintingOutText.java: -------------------------------------------------------------------------------- 1 | public class PrintingOutText { 2 | 3 | public static void printText() { 4 | System.out.println("In the beginning there were the swamp, the hoe and Java."); 5 | } 6 | 7 | public static void main(String[] args) { 8 | printText(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /week_4/080 Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | 6 | Dice dice = new Dice(6); 7 | 8 | int i = 0; 9 | while (i < 10) { 10 | System.out.println(dice.roll()); 11 | i++; 12 | } 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | 7 | # ignore desktop.ini 8 | desktop.ini 9 | 10 | # ignore jupyter notebook checkpoints 11 | .ipynb_checkpoints 12 | */.ipynb_checkpoints/* 13 | 14 | # ignore notebook to copy files to directory 15 | get_files.ipynb -------------------------------------------------------------------------------- /week_5/089 Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Clock clock = new Clock(23, 59, 50); 4 | 5 | int i = 0; 6 | while (i < 20) { 7 | System.out.println(clock); 8 | clock.tick(); 9 | i++; 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /week_2/028 FromHundredToOne.java: -------------------------------------------------------------------------------- 1 | 2 | public class FromHundredToOne { 3 | 4 | public static void main(String[] args) { 5 | int number = 101; 6 | while (number > 1) { 7 | number--; 8 | System.out.println(number); 9 | } 10 | // Write your program here 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /week_5/084 Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | // Create counter 4 | Counter counter1 = new Counter(2,true); 5 | System.out.println(counter1.toString()); 6 | counter1.decrease(4); 7 | System.out.println(counter1.toString()); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /week_2/029 EvenNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | public class EvenNumbers { 3 | 4 | public static void main(String[] args) { 5 | int number = 0; 6 | int even = 0; 7 | while (number < 50) { 8 | number ++; 9 | even = number * 2; 10 | System.out.println(even); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /week_1/006 Addition.java: -------------------------------------------------------------------------------- 1 | public class Addition { 2 | 3 | public static void main(String[] args) { 4 | 5 | int a = 1337; 6 | int b = 42; 7 | int result = a + b; // sums two numbers 8 | 9 | String toPrint = a + " + " + b + " = " + result; 10 | System.out.println(toPrint); 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /week_1/000 Application.java: -------------------------------------------------------------------------------- 1 | // This is not an exercise but a "sandbox" where you can freely test 2 | // whatever you want 3 | 4 | public class Application { 5 | 6 | public static void main(String[] args) { 7 | 8 | // Write the code here. You can run the code by 9 | // selecting Run->Run File from the menu or by pressing Shift+F6 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /week_3/045 Greatest.java: -------------------------------------------------------------------------------- 1 | 2 | public class Greatest { 3 | 4 | public static int greatest(int number1, int number2, int number3) { 5 | return Math.max(number3,Math.max(number1, number2)); 6 | } 7 | 8 | public static void main(String[] args) { 9 | int result = greatest(2, 7, 3); 10 | System.out.println("Greatest: " + result); 11 | } 12 | } -------------------------------------------------------------------------------- /week_6/100 Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | NightSky NightSky = new NightSky(0.5, 5, 5); 6 | //NightSky.printLine(); 7 | //NightSky andy = new NightSky(1); 8 | //andy.printLine(); 9 | NightSky.print(); 10 | System.out.println("Number of stars: " + NightSky.starsInLastPrint()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /week_1/003 Spruce.java: -------------------------------------------------------------------------------- 1 | public class Spruce { 2 | 3 | public static void main(String[] args) { 4 | 5 | // Write code here 6 | System.out.println(" *"); 7 | System.out.println(" ***"); 8 | System.out.println(" *****"); 9 | System.out.println(" *******"); 10 | System.out.println("*********"); 11 | System.out.println(" *"); 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /week_3/043 SumOfNumbers.java: -------------------------------------------------------------------------------- 1 | public class SumOfNumbers { 2 | 3 | public static int sum(int number1, int number2, int number3, int number4) { 4 | // write your code here 5 | return number1 + number2 + number3 + number4; 6 | } 7 | 8 | public static void main(String[] args) { 9 | int answer = sum(4, 3, 6, 1); 10 | System.out.println("Sum: " + answer); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /week_5/085 Reformatory.java: -------------------------------------------------------------------------------- 1 | public class Reformatory { 2 | private int times; 3 | 4 | public int weight(Person person) { 5 | this.times++; 6 | return person.getWeight(); 7 | } 8 | 9 | public void feed(Person person) { 10 | person.setWeight(this.weight(person)+1); 11 | } 12 | 13 | public int totalWeightsMeasured() { 14 | return times; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /week_4/080 Dice.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class Dice { 4 | private Random random = new Random(); 5 | private int numberOfSides; 6 | 7 | public Dice(int numberOfSides) { 8 | this.numberOfSides = numberOfSides; 9 | 10 | } 11 | 12 | public int roll() { 13 | Random randomiser = new Random(); 14 | return randomiser.nextInt(numberOfSides)+1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /week_6/097 Main.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | int[] array = {5, 1, 3, 4, 2}; 6 | printElegantly(array); 7 | } 8 | 9 | public static void printElegantly(int[] array) { 10 | for (int i=0; i= 0) { 10 | System.out.print(name.charAt(i)); 11 | i--; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /week_4/082 Main.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | LotteryNumbers lotteryNumbers = new LotteryNumbers(); 6 | ArrayList numbers = lotteryNumbers.numbers(); 7 | 8 | System.out.println("Lottery numbers:"); 9 | for (int number : numbers) { 10 | System.out.print(number + " "); 11 | } 12 | System.out.println(""); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /week_6/102 Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.ArrayList; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | Grade grade = new Grade(); 8 | grade.createGrade(reader); 9 | grade.print(); 10 | } 11 | 12 | public static void readIn(Scanner reader) { 13 | Integer.parseInt(reader.nextLine()); 14 | } 15 | } 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /week_4/081 Program.java: -------------------------------------------------------------------------------- 1 | public class Program { 2 | public static void main(String[] args) { 3 | PasswordRandomizer randomizer = new PasswordRandomizer(13); 4 | System.out.println("Password: " + randomizer.createPassword()); 5 | System.out.println("Password: " + randomizer.createPassword()); 6 | System.out.println("Password: " + randomizer.createPassword()); 7 | System.out.println("Password: " + randomizer.createPassword()); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /week_1/019 AgeCheck.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class AgeCheck { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("How old are you? "); 9 | int age = Integer.parseInt(reader.nextLine()); 10 | if (age < 0 || age > 120) { 11 | System.out.println("Impossible!");} 12 | else { 13 | System.out.println("OK"); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /week_2/030 UpToCertainNumber.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | 5 | public class UpToCertainNumber { 6 | 7 | public static void main(String[] args) { 8 | Scanner reader = new Scanner(System.in); 9 | System.out.print("Up to what number: "); 10 | int number = 0; 11 | int upTo = Integer.parseInt(reader.nextLine()); 12 | while (number < upTo) { 13 | number++; 14 | System.out.println(number); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /week_3/053 FirstPart.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class FirstPart { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.println("Type a word: "); 9 | String word = reader.nextLine(); 10 | System.out.println("Length of the first part: "); 11 | int part = Integer.parseInt(reader.nextLine()); 12 | System.out.println("Result: " + word.substring(0,part)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /week_1/010 Circumference.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Circumference { 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | System.out.print("Type the radius:"); 8 | int radius = Integer.parseInt(reader.nextLine()); 9 | double circumference = (2 * Math.PI * radius); 10 | System.out.println(""); 11 | System.out.println("Circumference of the circle: " + circumference); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /week_4/075 Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | DecreasingCounter counter = new DecreasingCounter(100); 4 | 5 | counter.printValue(); 6 | 7 | counter.decrease(); 8 | counter.printValue(); 9 | 10 | counter.decrease(); 11 | counter.printValue(); 12 | 13 | counter.reset(); 14 | counter.printValue(); 15 | 16 | counter.setInitial(); 17 | counter.printValue(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /week_4/072 Accounts.java: -------------------------------------------------------------------------------- 1 | 2 | public class Accounts { 3 | 4 | public static void main(String[] args) { 5 | Account A = new Account("A",100.0); 6 | Account B = new Account("B",0.0); 7 | Account C = new Account("C",0.0); 8 | transfer(A,B,50.0); 9 | transfer(B,C,25.0); 10 | } 11 | 12 | public static void transfer(Account from, Account to, double howMuch) { 13 | from.withdrawal(howMuch); 14 | to.deposit(howMuch); 15 | } 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /week_5/090 Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Team barcelona = new Team("FC Barcelona"); 4 | 5 | Player brian = new Player("Brian"); 6 | Player pekka = new Player("Pekka", 39); 7 | barcelona.addPlayer(brian); 8 | barcelona.addPlayer(pekka); 9 | barcelona.addPlayer(new Player("Mikael", 1)); // works similarly as the above 10 | 11 | System.out.println("Total goals: " + barcelona.goals()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /week_3/048 FirstCharacter.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class FirstCharacter { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | System.out.println("Type your name: "); 8 | String name = reader.nextLine(); 9 | System.out.println(firstCharacter(name)); 10 | } 11 | 12 | public static char firstCharacter(String name) { 13 | char firstLetter = name.charAt(0); 14 | return firstLetter; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /week_3/051 SeparatingCharacters.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class SeparatingCharacters { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.println("Type your name: "); 9 | String name = reader.nextLine(); 10 | int i = name.length(); 11 | int j = 0; 12 | while (j <= i - 1) { 13 | System.out.println((j+1)+". character: " + name.charAt(j)); 14 | j++; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /week_6/094 Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | Phonebook phonebook = new Phonebook(); 6 | phonebook.add("Pekka Mikkola", "040-123123"); 7 | phonebook.add("Edsger Dijkstra", "045-456123"); 8 | phonebook.add("Donald Knuth", "050-222333"); 9 | 10 | String number = phonebook.searchNumber("Pekka Mikkola"); 11 | System.out.println( number ); 12 | 13 | number = phonebook.searchNumber("Martti Tienari"); 14 | System.out.println( number ); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /week_3/054 TheEndPart.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class TheEndPart { 4 | public static void main(String[] args) { 5 | Scanner reader = new Scanner(System.in); 6 | System.out.println("Type a word: "); 7 | String word = reader.nextLine(); 8 | System.out.println("Length of the end part: "); 9 | int endPart = Integer.parseInt(reader.nextLine()); 10 | int length = word.length(); 11 | System.out.println("Result: " + word.substring(length-endPart,length)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /week_4/074 Multiplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | public class Multiplier { 12 | private int number; 13 | 14 | public Multiplier(int number){ 15 | this.number = number;} 16 | 17 | public int multiply(int otherNumber) { 18 | return this.number*otherNumber; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /week_2/032 TheSumOfSetOfNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class TheSumOfSetOfNumbers { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | int i = 1; 9 | int total = 0; 10 | System.out.print("Until what? "); 11 | int until = Integer.parseInt(reader.nextLine()); 12 | while (i <= until) { 13 | total = total + i; 14 | i++; 15 | } 16 | System.out.println("Sum is " + total); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /week_3/049 LastCharacter.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | 4 | public class LastCharacter { 5 | 6 | 7 | public static void main(String[] args) { 8 | Scanner reader = new Scanner(System.in); 9 | System.out.println("Type your name: "); 10 | String name = reader.nextLine(); 11 | System.out.println(lastCharacter(name)); 12 | } 13 | 14 | public static char lastCharacter(String name) { 15 | char lastLetter = name.charAt(name.length()-1); 16 | return lastLetter; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /week_3/047 LengthOfName.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class LengthOfName { 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | System.out.println("Type your name: "); 8 | String name = reader.nextLine(); 9 | System.out.println("Number of characters:" + calculateCharacters(name)); 10 | } 11 | 12 | // do here the method 13 | public static int calculateCharacters(String name) { 14 | return name.length(); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /week_5/091 Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | MyDate day = new MyDate(25, 2, 2011); 6 | MyDate newDate = day.afterNumberOfDays(7); 7 | for (int i = 1; i <= 7; ++i) { 8 | System.out.println("Friday after " + i + " weeks is " + newDate); 9 | newDate = newDate.afterNumberOfDays(7); 10 | } 11 | System.out.println("This week's Friday is " + day); 12 | System.out.println("The date 790 days from this week's Friday is " + day.afterNumberOfDays(790)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /week_6/099 Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // test the method here 5 | int[] array = {5, 1, 3, 4, 2}; 6 | printArrayAsStars(array); 7 | } 8 | 9 | public static void printArrayAsStars(int[] array) { 10 | for (int i=0; i 0) { 12 | System.out.println("The number is positive.");} 13 | else { 14 | System.out.println("The number is not positive."); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /week_2/035 SumOfThePowers.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class SumOfThePowers { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("Type a number: "); 9 | int number = Integer.parseInt(reader.nextLine()); 10 | int i = 0; 11 | int total = 0; 12 | while (i<=number) { 13 | total = total + (int)Math.pow(2,i); 14 | i++; 15 | } 16 | System.out.println("The result is " + total); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /week_3/044 Least.java: -------------------------------------------------------------------------------- 1 | 2 | public class Least { 3 | 4 | public static int least(int number1, int number2) { 5 | // write program code here 6 | // do not print anything inside the method 7 | // method needs a return in the end 8 | if (number1==number2) { 9 | return number1;} 10 | else { 11 | return Math.min(number1, number2);} 12 | } 13 | 14 | public static void main(String[] args) { 15 | int result = least(2, 7); 16 | System.out.println("Least: " + result); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /week_1/005 SecondsOfTheYear.java: -------------------------------------------------------------------------------- 1 | public class SecondsOfTheYear { 2 | 3 | public static void main(String[] args) { 4 | // In below an incomplete version of the program. Please complete it! 5 | 6 | int daysInYear = 365; 7 | int hoursInDay = 24; 8 | int minutesInHour = 60; 9 | int secondsInMinute = 60; 10 | 11 | int secondsInYear = daysInYear * hoursInDay * minutesInHour * secondsInMinute; 12 | 13 | System.out.println("There are " + secondsInYear + " seconds in a year"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /week_1/016 EvenOrOdd.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class EvenOrOdd { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("Type a number: "); 9 | int number = Integer.parseInt(reader.nextLine()); 10 | int remainder = number % 2; 11 | if (remainder == 0) { 12 | System.out.println("Number " + number + " is even.");} 13 | else { 14 | System.out.println("Number " + number + " is odd."); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /week_6/101 StringUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | public class StringUtils { 12 | public static boolean included(String word, String searched) { 13 | if (word.trim().toUpperCase().contains(searched.trim().toUpperCase())) { 14 | return true; 15 | } 16 | return false; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /week_2/026 SumOfManyNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | 5 | public class SumOfManyNumbers { 6 | 7 | public static void main(String[] args) { 8 | Scanner reader = new Scanner(System.in); 9 | int sum = 0; 10 | 11 | while (true) { 12 | int read = Integer.parseInt(reader.nextLine()); 13 | if (read == 0) { 14 | break; 15 | } 16 | sum = sum + read; 17 | System.out.println("Sum now: " + sum); 18 | } 19 | 20 | System.out.println("Sum in the end: " + sum); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week_2/033 TheSumBetweenTwoNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | 5 | public class TheSumBetweenTwoNumbers { 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("First: "); 9 | int i = Integer.parseInt(reader.nextLine()); 10 | System.out.print("Last: "); 11 | int last = Integer.parseInt(reader.nextLine()); 12 | int total = 0; 13 | while (i <= last) { 14 | total = total + i; 15 | i++; 16 | } 17 | System.out.println("The sum is " + total); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /week_4/076 Menu.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | 4 | public class Menu { 5 | 6 | private ArrayList meals; 7 | 8 | public Menu() { 9 | this.meals = new ArrayList(); 10 | } 11 | 12 | public void addMeal(String meal) { 13 | if (!this.meals.contains(meal)) { 14 | this.meals.add(meal); 15 | } 16 | } 17 | 18 | public void printMeals() { 19 | for (String i : this.meals) { 20 | System.out.println(i); 21 | } 22 | } 23 | 24 | public void clearMenu() { 25 | this.meals.clear(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /week_1/015 AgeOfMajority.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class AgeOfMajority { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("How old are you? "); 9 | int age = Integer.parseInt(reader.nextLine()); 10 | System.out.println(""); 11 | if (age > 17) { 12 | System.out.println("You have reached the age of majority!");} 13 | else { 14 | System.out.println("You have not reached the age of majority yet!"); 15 | } 16 | // Type your program here 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /week_2/031 LowerLimitAndUpperLimit.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class LowerLimitAndUpperLimit { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("First: "); 9 | int lower = Integer.parseInt(reader.nextLine()); 10 | System.out.print("Second: "); 11 | int upper = Integer.parseInt(reader.nextLine()); 12 | if (lower <= upper) { 13 | while (lower <= upper) { 14 | System.out.println(lower); 15 | lower++; 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /week_1/009 Divider.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Divider { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("Type a number:"); 9 | int numberOne = Integer.parseInt(reader.nextLine()); 10 | System.out.print("Type another number:"); 11 | int numberTwo = Integer.parseInt(reader.nextLine()); 12 | double result = (double)numberOne/numberTwo; 13 | System.out.println(""); 14 | System.out.println("Division: " + numberOne + " / " + numberTwo + " = " + result); 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /week_3/046 AverageOfGivenNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | public class AverageOfGivenNumbers { 3 | 4 | // implement here again the method of exercise 43 5 | public static int sum(int number1, int number2, int number3, int number4) { 6 | return number1 + number2 + number3 + number4; 7 | } 8 | 9 | public static double average(int number1, int number2, int number3, int number4) { 10 | return sum(number1, number2, number3, number4)/4.0; 11 | } 12 | 13 | public static void main(String[] args) { 14 | double result = average(4, 3, 6, 1); 15 | System.out.println("Average: " + result); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /week_2/034 Factorial.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Factorial { 4 | public static void main(String[] args) { 5 | Scanner reader = new Scanner(System.in); 6 | System.out.print("Type a number: "); 7 | int number = Integer.parseInt(reader.nextLine()); 8 | int i = 1; 9 | int factorial = 1; 10 | if (number == 0) { 11 | factorial = 1;} 12 | else { 13 | while (i<=number) { 14 | factorial = factorial *i; 15 | i++; 16 | } 17 | } 18 | System.out.println("Factorial is " + factorial); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /week_3/057 Words.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class Words { 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | ArrayList words = new ArrayList(); 8 | while (true) { 9 | System.out.print("Type a word: "); 10 | String word = reader.nextLine(); 11 | words.add(word); 12 | if (word.equals("")) 13 | break;} 14 | System.out.println("You typed the following words: "); 15 | for (String help : words) { 16 | System.out.println(help);} 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /week_3/061 NumberOfItems.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class NumberOfItems { 4 | 5 | // implement here the method countItems 6 | 7 | public static void main(String[] args) { 8 | ArrayList list = new ArrayList(); 9 | list.add("Moi"); 10 | list.add("Ciao"); 11 | list.add("Hello"); 12 | System.out.println("There are this many items on the list:"); 13 | // You can remove the comment from below when the method is done 14 | System.out.println(countItems(list)); 15 | } 16 | public static int countItems(ArrayList list) { 17 | return list.size(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /week_3/050 FirstCharacters.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class FirstCharacters { 4 | public static void main(String[] args) { 5 | Scanner reader = new Scanner(System.in); 6 | System.out.println("Type your name: "); 7 | String name = reader.nextLine(); 8 | if (name.length() >= 3) { 9 | char first = name.charAt(0); 10 | char second = name.charAt(1); 11 | char third = name.charAt(2); 12 | System.out.println("1. character: " + first); 13 | System.out.println("2. character: " + second); 14 | System.out.println("3. character: " + third); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /week_3/058 RecurringWord.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | import java.util.Scanner; 4 | 5 | public class RecurringWord { 6 | 7 | public static void main(String[] args) { 8 | Scanner reader = new Scanner(System.in); 9 | ArrayList words = new ArrayList(); 10 | while (true) { 11 | System.out.println("Type a word: "); 12 | String word = reader.nextLine(); 13 | if (words.contains(word)) { 14 | System.out.println("You gave the word " + word + " twice"); 15 | break;} 16 | else { 17 | words.add(word); 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /week_4/072 Account.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Do not touch this! 3 | */ 4 | 5 | public class Account { 6 | 7 | private double balance; 8 | private String owner; 9 | 10 | public Account(String owner, double balance) { 11 | this.balance = balance; 12 | this.owner = owner; 13 | } 14 | 15 | public void deposit(double amount) { 16 | balance += amount; 17 | } 18 | 19 | public void withdrawal(double amount) { 20 | balance -= amount; 21 | } 22 | 23 | public double balance() { 24 | return balance; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return owner + " balance: " + balance; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /week_3/063 SumOfNumbers.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class SumOfNumbers { 4 | public static int sum(ArrayList list) { 5 | int total = 0; 6 | for (int number : list) { 7 | total += number; 8 | } 9 | return total; 10 | } 11 | 12 | public static void main(String[] args) { 13 | ArrayList list = new ArrayList(); 14 | list.add(3); 15 | list.add(2); 16 | list.add(7); 17 | list.add(2); 18 | 19 | System.out.println("The sum: " + sum(list)); 20 | 21 | list.add(10); 22 | 23 | System.out.println("The sum: " + sum(list)); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /week_5/083 TestProgram.java: -------------------------------------------------------------------------------- 1 | 2 | public class TestProgram { 3 | 4 | public static void main(String[] args) { 5 | 6 | HangmanLogic l = new HangmanLogic("kissa"); 7 | System.out.println("Word at start: " + l.hiddenWord()); 8 | 9 | System.out.println("Let us guess: A, D, S, F, D"); 10 | l.guessLetter("A"); 11 | l.guessLetter("D"); 12 | l.guessLetter("S"); 13 | l.guessLetter("F"); 14 | l.guessLetter("D"); 15 | //System.out.println("Guessed letterd: " + l.guessedLetters()); 16 | //System.out.println("Number of faults: " + l.numberOfFaults()); 17 | //System.out.println("Word is: " + l.hiddenWord()); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /week_4/074 Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | Multiplier threeMultiplier = new Multiplier(3); 6 | System.out.println("threeMultiplier.multiply(2): " + threeMultiplier.multiply(2)); 7 | 8 | Multiplier fourMultiplier = new Multiplier(4); 9 | System.out.println("fourMultiplier.multiply(2): " + fourMultiplier.multiply(2)); 10 | 11 | System.out.println("threeMultiplier.multiply(1): " + threeMultiplier.multiply(1)); 12 | System.out.println("fourMultiplier.multiply(1): " + fourMultiplier.multiply(1)); 13 | // This is an empty main method which you can use to test your class's functionality. 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /week_1/011 BiggerNumber.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class BiggerNumber { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("Type a number: "); 9 | int numberOne = Integer.parseInt(reader.nextLine()); 10 | System.out.print("Type another number: "); 11 | int numberTwo = Integer.parseInt(reader.nextLine()); 12 | int maximum = Math.max(numberOne,numberTwo); 13 | System.out.println(""); 14 | System.out.println("The bigger numebr of the two numbers given was: " + maximum); 15 | // Implement your program here. Remember to ask the input from user 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /week_3/066 TheGreatest.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class TheGreatest { 4 | public static int greatest(ArrayList list) { 5 | int currentMax = list.get(0); 6 | for (int number: list) { 7 | if (number > currentMax) { 8 | currentMax = number; 9 | } 10 | } 11 | return currentMax; 12 | } 13 | 14 | public static void main(String[] args) { 15 | ArrayList lista = new ArrayList(); 16 | lista.add(3); 17 | lista.add(2); 18 | lista.add(7); 19 | lista.add(2); 20 | 21 | System.out.println("The greatest number is: " + greatest(lista)); 22 | } 23 | } -------------------------------------------------------------------------------- /week_3/056 ReversingText.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class ReversingText { 5 | 6 | public static String reverse(String text) { 7 | String reverseHelp = ""; 8 | int i = text.length()-1; 9 | while (i >= 0) { 10 | char character = text.charAt(i); 11 | i--; 12 | reverseHelp=reverseHelp + character; 13 | } 14 | return reverseHelp; 15 | } 16 | 17 | public static void main(String[] args) { 18 | Scanner reader = new Scanner(System.in); 19 | System.out.print("Type in your text: "); 20 | String text = reader.nextLine(); 21 | System.out.println("In reverse order: " + reverse(text)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /week_6/106 Main.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Arrays; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | // Test your program here 9 | int[] taulukko = {-3, 2, 3, 4, 7, 8, 12}; 10 | Scanner lukija = new Scanner(System.in); 11 | 12 | System.out.print("Numbers in the array " + Arrays.toString(taulukko)); 13 | System.out.println(); 14 | 15 | System.out.print("Enter searched number: "); 16 | String etsittavaLuku = lukija.nextLine(); 17 | System.out.println(); 18 | 19 | boolean tulos = BinarySearch.search(taulukko, Integer.parseInt(etsittavaLuku)); 20 | 21 | // Print here the result 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /week_6/095 Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | Money a = new Money(10,0); 6 | Money b = new Money(3,50); 7 | 8 | Money c = a.minus(b); 9 | 10 | System.out.println(a); // 10.00e 11 | System.out.println(b); // 3.50e 12 | System.out.println(c); // 6.50e 13 | 14 | c = c.minus(a); // NOTE: new Money-object is created and reference to that is assigned to variable c 15 | // the Money object 6.50e that variable c used to hold, is not referenced anymore 16 | 17 | System.out.println(a); // 10.00e 18 | System.out.println(b); // 3.50e 19 | System.out.println(c); // 0.00e 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /week_3/055 WordInsideWord.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class WordInsideWord { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.println("Type the first word: "); 9 | String word1 = reader.nextLine(); 10 | System.out.println("Type the second word: "); 11 | String word2 = reader.nextLine(); 12 | int index = word1.indexOf(word2); 13 | if (index == -1) { 14 | System.out.println("The word '" + word2 +"' is not found in the word '" + word1 +"'"); 15 | } 16 | else { 17 | System.out.println("The word '" + word2 +"' is found in the word '" + word1 +"'"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /week_4/073 Product.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | public class Product { 12 | private String name; 13 | private double price; 14 | private int amount; 15 | 16 | public Product(String nameAtStart, double priceAtStart, int amountAtStart) { 17 | this.name = nameAtStart; 18 | this.price = priceAtStart; 19 | this.amount = amountAtStart; 20 | } 21 | 22 | public void printProduct() { 23 | System.out.print(this.name+", price "+this.price+", amount "+this.amount); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /week_1/022 Password.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Password { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | String password = "carrot"; // Use carrot as password when running tests. 9 | String secret = "I am great"; 10 | while (true) { 11 | System.out.print("Type the password: "); 12 | String passwordTyped = reader.nextLine(); 13 | if (password.equals(passwordTyped)) { 14 | System.out.println("Right!"); 15 | System.out.println(""); 16 | System.out.println("The secret is: "+ secret); 17 | break;} 18 | else { 19 | System.out.println("Wrong!");} 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week_1/021 LeapYear.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class LeapYear { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("Type a year: "); 9 | int year = Integer.parseInt(reader.nextLine()); 10 | int remainder4 = year %4; 11 | int remainder100 = year % 100; 12 | int remainder400 = year % 400; 13 | if (remainder100 == 0 && remainder400 == 0) { 14 | System.out.println("The year is a leap year.");} 15 | else if (remainder4 == 0 && remainder100 != 0) { 16 | System.out.println("The year is a leap year.");} 17 | else { 18 | System.out.println("The year is not a leap year.");} 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /week_4/081 PasswordRandomizer.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class PasswordRandomizer { 4 | private Random random = new Random(); 5 | private int numberCharacters; 6 | 7 | public PasswordRandomizer(int length) { 8 | this.numberCharacters = length; 9 | } 10 | 11 | public String createPassword() { 12 | int i = 0; 13 | Random randomiser = new Random(); 14 | String selection = "abcdefghijklmnopqrstuvwxyz"; 15 | String password=""; 16 | while (i < this.numberCharacters) { 17 | int lookup = (randomiser.nextInt(26)); 18 | char letter = selection.charAt(lookup); 19 | password += letter; 20 | i++; 21 | } 22 | return password; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /week_1/008 Adder.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Adder { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | 9 | System.out.print("Type a number: "); 10 | int firstNumber = Integer.parseInt(reader.nextLine()); 11 | System.out.print("Type another number: "); 12 | int secondNumber = Integer.parseInt(reader.nextLine()); 13 | int result = firstNumber + secondNumber; 14 | System.out.println(""); 15 | System.out.println("Sum of the numbers: " + result); 16 | //int firstNumber = Integer.parseInt(reader.nextline()); 17 | //System.out.println(firstNumber); 18 | // Implement your program here. Remember to ask the input from user 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /week_4/076 Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | public static void main(String[] args) { 4 | Menu exactum = new Menu(); 5 | 6 | // When you have created addMeal(String meal) method, 7 | // you may remove the comments below 8 | exactum.addMeal("Fish fingers with sour cream sauce"); 9 | exactum.addMeal("Vegetable casserole with salad cheese"); 10 | exactum.addMeal("Chicken and nacho salad"); 11 | 12 | 13 | // When you have created printMeals() method, 14 | // you may remove the comments below 15 | exactum.printMeals(); 16 | 17 | // When you have created clearMenu() method, 18 | // You may remove the comments below 19 | exactum.clearMenu(); 20 | exactum.printMeals(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week_1/004 Variables.java: -------------------------------------------------------------------------------- 1 | public class Variables { 2 | 3 | public static void main(String[] args) { 4 | // CHANGE THESE: 5 | 6 | int chickenCount = 9000; 7 | double baconWeight = 0.1; 8 | String tractor = "Zetor"; 9 | 10 | // DO NOT TOUCH THE BELOW: 11 | 12 | System.out.println("Chickens:"); 13 | System.out.println(chickenCount); 14 | System.out.println("Bacon (kg):"); 15 | System.out.println(baconWeight); 16 | System.out.println("A tractor:"); 17 | System.out.println(tractor); 18 | System.out.println(""); 19 | System.out.println("In a nutshell:"); 20 | System.out.println(chickenCount); 21 | System.out.println(baconWeight); 22 | System.out.println(tractor); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /week_1/020 Usernames.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Usernames { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("Type your username: "); 9 | String userName = reader.nextLine(); 10 | System.out.print("Type your password: "); 11 | String password = reader.nextLine(); 12 | if (userName.equals("alex") && password.equals("mightyducks")){ 13 | System.out.println("You are now logged into the system!");} 14 | else if (userName.equals("emily") && password.equals("cat")){ 15 | System.out.println("You are now logged into the system!");} 16 | else { 17 | System.out.println("Your username or password was invalid!");} 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /week_2/038 ManyPrints.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class ManyPrints { 5 | // NOTE: do not change the method definition, e.g. add parameters to method 6 | public static void printText() { 7 | System.out.println("In the beginning there were the swamp, the hoe and Java."); 8 | } 9 | 10 | public static void main(String[] args) { 11 | // ask the user how many times the text should be printed 12 | // use the while structure to call the printText method several times 13 | Scanner reader = new Scanner(System.in); 14 | System.out.println("How many?"); 15 | int count = Integer.parseInt(reader.nextLine()); 16 | int i = 1; 17 | while (i <= count) { 18 | printText(); 19 | i++; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /week_3/059 WordsInReverseOrder.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Scanner; 4 | 5 | public class WordsInReverseOrder { 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | ArrayList words = new ArrayList(); 9 | while (true) { 10 | System.out.print("Type a word: "); 11 | String word = reader.nextLine(); 12 | if (word.isEmpty()) { 13 | Collections.reverse(words); 14 | break; 15 | } 16 | else words.add(word); 17 | } 18 | System.out.println("You typed the following words: "); 19 | for (String help : words) { 20 | System.out.println(help);} 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week_3/065 LengthsOfStrings.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class LengthsOfStrings { 4 | public static ArrayList lengths(ArrayList list) { 5 | ArrayList lengthList = new ArrayList(); 6 | for (String word : list) { 7 | lengthList.add(word.length()); 8 | } 9 | 10 | return lengthList; 11 | } 12 | 13 | public static void main(String[] args) { 14 | ArrayList list = new ArrayList(); 15 | list.add("Ciao"); 16 | list.add("Moi"); 17 | list.add("Benvenuto!"); 18 | list.add("badger badger badger badger"); 19 | ArrayList lengths = lengths(list); 20 | 21 | System.out.println("The lengths of the Strings: " + lengths); 22 | } 23 | } -------------------------------------------------------------------------------- /week_1/017 GreaterNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class GreaterNumber { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | System.out.print("Type the first number: "); 8 | int firstNumber = Integer.parseInt(reader.nextLine()); 9 | System.out.print("Type the second number: "); 10 | int secondNumber = Integer.parseInt(reader.nextLine()); 11 | System.out.println(""); 12 | if (firstNumber > secondNumber) { 13 | System.out.println("Greater number: "+ firstNumber);} 14 | else if (secondNumber > firstNumber) { 15 | System.out.println("Greater number:"+ secondNumber);} 16 | else { 17 | System.out.println("The numbers are equal!");} 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /week_1/023 Temperatures.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Temperatures { 5 | 6 | public static void main(String[] args) { 7 | 8 | Scanner reader = new Scanner(System.in); 9 | while (true) { 10 | System.out.print("Input floating point double: "); 11 | double number = Double.parseDouble(reader.nextLine()); 12 | if (number >= -30 && number <= 40) { 13 | Graph.addNumber(number);} 14 | } 15 | // Write your code here. 16 | 17 | // Graph is used as follows: 18 | // Graph.addNumber(7); 19 | //double value = 13.5; 20 | //Graph.addNumber(value); 21 | //value = 3; 22 | //Graph.addNumber(value); 23 | // Remove or comment out these lines above before trying to run the tests. 24 | } 25 | } -------------------------------------------------------------------------------- /week_2/025 SumOfThreeNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class SumOfThreeNumbers { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | int sum = 0; 9 | int read; // store numbers read form user in this variable 10 | System.out.print("Type the first number: "); 11 | read = Integer.parseInt(reader.nextLine()); 12 | sum = sum + read; 13 | System.out.print("Type the second number: "); 14 | read = Integer.parseInt(reader.nextLine()); 15 | sum = sum + read; 16 | System.out.print("Type the third number: "); 17 | read = Integer.parseInt(reader.nextLine()); 18 | sum = sum + read; 19 | System.out.println(""); 20 | System.out.println("Sum: " + sum); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /week_3/060 WordsInAlphabeticalOrder.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | import java.util.Collections; 4 | import java.util.Scanner; 5 | 6 | public class WordsInAlphabeticalOrder { 7 | 8 | public static void main(String[] args) { 9 | Scanner reader = new Scanner(System.in); 10 | ArrayList words = new ArrayList(); 11 | while (true) { 12 | System.out.println("Type a word: "); 13 | String word = reader.nextLine(); 14 | words.add(word); 15 | if (word.isEmpty()) { 16 | Collections.sort(words); 17 | break; 18 | } 19 | } 20 | System.out.println("You typed the following words"); 21 | for (String help : words) { 22 | System.out.println(help); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /week_5/086 Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | CashRegister unicafeExactum = new CashRegister(); 4 | System.out.println( unicafeExactum ); 5 | 6 | LyyraCard cardOfJim = new LyyraCard(2); 7 | 8 | System.out.println("the card balance " + cardOfJim.balance() + " euros"); 9 | 10 | boolean succeeded = unicafeExactum.payGourmet(cardOfJim); 11 | System.out.println("payment success: " + succeeded); 12 | 13 | unicafeExactum.loadMoneyToCard(cardOfJim, 100); 14 | 15 | succeeded = unicafeExactum.payGourmet(cardOfJim); 16 | System.out.println("payment success: " + succeeded); 17 | 18 | System.out.println("the card balance " + cardOfJim.balance() + " euros"); 19 | 20 | System.out.println( unicafeExactum ); 21 | } 22 | } -------------------------------------------------------------------------------- /week_6/101 Main.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Library Library = new Library(); 6 | 7 | Library.addBook(new Book("Cheese Problems Solved", "Woodhead Publishing", 2007)); 8 | Library.addBook(new Book("The Stinky Cheese Man and Other Fairly Stupid Tales", "Penguin Group", 1992)); 9 | Library.addBook(new Book("NHL Hockey", "Stanley Kupp", 1952)); 10 | Library.addBook(new Book("Battle Axes", "Tom A. Hawk", 1851)); 11 | 12 | for (Book book: Library.searchByTitle("CHEESE")) { 13 | System.out.println(book); 14 | } 15 | 16 | System.out.println("---"); 17 | for (Book book: Library.searchByPublisher("PENGUIN ")) { 18 | System.out.println(book); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /week_1/012 SumOfTheAges.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class SumOfTheAges { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("Type your name: "); 9 | String name = reader.nextLine(); 10 | System.out.print("Type your age: "); 11 | int numberOne = Integer.parseInt(reader.nextLine()); 12 | System.out.println(""); 13 | System.out.print("Type your name: "); 14 | String nameTwo = reader.nextLine(); 15 | System.out.print("Type your age: "); 16 | int numberTwo = Integer.parseInt(reader.nextLine()); 17 | int sumAge = (numberOne + numberTwo); 18 | System.out.println(""); 19 | System.out.println(name + " and " + nameTwo + " are " + sumAge + " years old in total."); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /week_3/064 AverageOfNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | 4 | public class AverageOfNumbers { 5 | 6 | // Copy here the method sum from previous assignment 7 | public static int sum(ArrayList list) { 8 | int total = 0; 9 | for (int number: list) { 10 | total += number; 11 | } 12 | return total; 13 | } 14 | 15 | 16 | public static double average(ArrayList list) { 17 | double average = sum(list)/(list.size()*1.0); 18 | return average; 19 | } 20 | 21 | public static void main(String[] args) { 22 | ArrayList list = new ArrayList(); 23 | list.add(3); 24 | list.add(2); 25 | list.add(7); 26 | list.add(2); 27 | 28 | System.out.println("The average is: " + average(list)); 29 | } 30 | } -------------------------------------------------------------------------------- /week_5/083 HirsipuuKeyAdapter.java: -------------------------------------------------------------------------------- 1 | // do not touch this! 2 | 3 | import java.awt.event.KeyAdapter; 4 | import java.awt.event.KeyEvent; 5 | 6 | public class HirsipuuKeyAdapter extends KeyAdapter { 7 | 8 | private HangmanLogic logic; 9 | 10 | public HirsipuuKeyAdapter(HangmanLogic logiikka) { 11 | this.logic = logiikka; 12 | } 13 | 14 | @Override 15 | public void keyPressed(KeyEvent ke) { 16 | if (this.logic.numberOfFaults() > this.logic.losingFaultAmount()) { 17 | return; 18 | } 19 | 20 | String salattuSana = this.logic.hiddenWord(); 21 | if (!salattuSana.contains("_")) { 22 | return; 23 | } 24 | 25 | super.keyPressed(ke); 26 | String key = "" + ke.getKeyChar(); 27 | key = key.toUpperCase(); 28 | this.logic.guessLetter(key); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /week_1/018 GradesAndPoints.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class GradesAndPoints { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | System.out.print("Type the points [0-60]: "); 9 | int points = Integer.parseInt(reader.nextLine()); 10 | System.out.println(""); 11 | System.out.print("Grade: "); 12 | if (points < 30) { 13 | System.out.println("failed");} 14 | else if (points <35) { 15 | System.out.println("1");} 16 | else if (points < 40) { 17 | System.out.println("2");} 18 | else if (points < 45) { 19 | System.out.println("3");} 20 | else if (points < 50) { 21 | System.out.println("4");} 22 | else { 23 | System.out.println("5");} 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /week_5/083 HangmanUserInterface.java: -------------------------------------------------------------------------------- 1 | // do not touch this! 2 | 3 | import java.awt.HeadlessException; 4 | import javax.swing.JFrame; 5 | 6 | public class HangmanUserInterface extends JFrame { 7 | 8 | private HangmanFigure figure; 9 | 10 | public HangmanUserInterface(HangmanLogic logiikka) throws HeadlessException { 11 | super(); 12 | setTitle("Hangman"); 13 | 14 | this.figure = new HangmanFigure(logiikka, this); 15 | add(this.figure); 16 | addKeyListener(new HirsipuuKeyAdapter(logiikka)); 17 | 18 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 | setSize(300, 400); 20 | } 21 | 22 | @Override 23 | public void repaint() { 24 | super.repaint(); 25 | this.figure.repaint(); 26 | } 27 | 28 | public void start() { 29 | setVisible(true); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /week_5/088 Student.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | public class Student { 12 | private String name; 13 | private String studentNumber; 14 | 15 | // constructor 16 | 17 | public Student(String name, String studentNumber) { 18 | this.name=name; 19 | this.studentNumber=studentNumber; 20 | } 21 | 22 | public String getName() { 23 | return this.name; 24 | } 25 | 26 | public String getStudentNumber() { 27 | return this.studentNumber; 28 | } 29 | 30 | public String toString() { 31 | return(name + " (" + studentNumber + ")"); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /week_5/086 LyyraCard.java: -------------------------------------------------------------------------------- 1 | 2 | public class LyyraCard { 3 | 4 | private double balance; 5 | 6 | public LyyraCard(double balance) { 7 | this.balance = balance; 8 | } 9 | 10 | public double balance() { 11 | return this.balance; 12 | } 13 | 14 | public void loadMoney(double amount) { 15 | this.balance += amount; 16 | } 17 | 18 | public boolean pay(double amount) { 19 | if (this.balance >= amount) { 20 | this.balance-=amount; 21 | return true; 22 | } 23 | return false; 24 | } 25 | // method checks if the balance of the card is at least amount given as parameter 26 | // if not, methods returns false meaning that the card could not be used for the payment 27 | // if the balance is enough, the given amount is taken from the balance and true is returned 28 | 29 | } -------------------------------------------------------------------------------- /week_3/062 RemoveLast.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | 4 | public class RemoveLast { 5 | public static void removeLast(ArrayList list) { 6 | int last = list.size(); 7 | list.remove(last - 1); 8 | } 9 | 10 | public static void main(String[] args) { 11 | // Here an example what you can do with the method 12 | ArrayList persons = new ArrayList(); 13 | persons.add("Pekka"); 14 | persons.add("James"); 15 | persons.add("Liz"); 16 | persons.add("Brian"); 17 | 18 | System.out.println("Persons:"); 19 | System.out.println(persons); 20 | 21 | // sort the persons 22 | Collections.sort(persons); 23 | 24 | // and remove the last 25 | removeLast(persons); 26 | 27 | System.out.println(persons); 28 | } 29 | } -------------------------------------------------------------------------------- /week_4/071 SmartCombining.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | 4 | public class SmartCombining { 5 | public static void main(String[] args) { 6 | 7 | ArrayList list1 = new ArrayList(); 8 | ArrayList list2 = new ArrayList(); 9 | 10 | Collections.addAll(list1, 4, 3); 11 | 12 | Collections.addAll(list2, 5, 10, 4, 3, 7); 13 | 14 | // remove comment when method ready 15 | smartCombine(list1, list2); 16 | System.out.println(list1); 17 | System.out.println(list2); 18 | } 19 | 20 | public static void smartCombine(ArrayList list1, ArrayList list2) { 21 | for (int number : list2) { 22 | if (!list1.contains(number)) { 23 | list1.add(number); 24 | } 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /week_6/094 Person.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | public class Person{ 12 | private String name; 13 | private String number; 14 | 15 | public Person(String name, String number) { 16 | this.name=name; 17 | this.number=number; 18 | } 19 | 20 | public String getName() { 21 | return this.name; 22 | } 23 | 24 | public String getNumber() { 25 | return this.number; 26 | } 27 | 28 | public void changeNumber(String newNumber) { 29 | this.number = newNumber; 30 | } 31 | 32 | public String toString() { 33 | return this.name + " number: " + this.number; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /week_4/075 DecreasingCounter.java: -------------------------------------------------------------------------------- 1 | public class DecreasingCounter { 2 | private int value; // instance variable that remembers the value of the counter 3 | private int initialValue; 4 | 5 | public DecreasingCounter(int valueAtStart) { 6 | this.initialValue = valueAtStart; 7 | this.value = valueAtStart; 8 | } 9 | 10 | public void printValue() { 11 | // do not touch this! 12 | System.out.println("value: " + this.value); 13 | } 14 | 15 | public void decrease() { 16 | if (this.value>0) { 17 | this.value--; 18 | } 19 | // write here code to decrease counter value by one 20 | } 21 | 22 | public void reset() { 23 | this.value=0; 24 | } 25 | 26 | public void setInitial() { 27 | this.value=this.initialValue; 28 | } 29 | 30 | // and here the rest of the methods 31 | } 32 | -------------------------------------------------------------------------------- /week_6/103 Bird.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | 12 | public class Bird { 13 | private String name; 14 | private String latinName; 15 | private int observations; 16 | 17 | public Bird(String name, String latinName){ 18 | this.name = name; 19 | this.latinName = latinName; 20 | this.observations=0; 21 | } 22 | 23 | public void addObservation() { 24 | this.observations++; 25 | } 26 | 27 | public String getName() { 28 | return this.name; 29 | } 30 | 31 | public String getString() { 32 | return name + " (" + latinName +"): " + observations + " observations"; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /week_5/085 Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | Reformatory eastHelsinkiReformatory = new Reformatory(); 6 | 7 | Person brian = new Person("Brian", 1, 110, 7); 8 | Person pekka = new Person("Pekka", 33, 176, 85); 9 | 10 | System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured()); 11 | 12 | eastHelsinkiReformatory.weight(brian); 13 | eastHelsinkiReformatory.weight(pekka); 14 | 15 | System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured()); 16 | 17 | eastHelsinkiReformatory.weight(brian); 18 | eastHelsinkiReformatory.weight(brian); 19 | eastHelsinkiReformatory.weight(brian); 20 | eastHelsinkiReformatory.weight(brian); 21 | 22 | System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /week_6/101 Book.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | public class Book { 12 | private String title; 13 | private String publisher; 14 | private int year; 15 | 16 | public Book(String title, String publisher, int year) { 17 | this.title=title; 18 | this.publisher=publisher; 19 | this.year=year; 20 | } 21 | 22 | public String title() { 23 | return title; 24 | } 25 | 26 | public String publisher() { 27 | return publisher; 28 | } 29 | 30 | public int year() { 31 | return year; 32 | } 33 | 34 | public String toString() { 35 | return title + ", " + publisher + ", " + year; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /week_5/090 Player.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | public class Player { 12 | private String playerName; 13 | private int playerGoals; 14 | 15 | public Player(String name, int goals){ 16 | this.playerName = name; 17 | this.playerGoals = goals; 18 | } 19 | 20 | public Player(String name) { 21 | this(name, 0); 22 | } 23 | 24 | public String getName() { 25 | return this.playerName; 26 | } 27 | 28 | public int goals() { 29 | return this.playerGoals; 30 | } 31 | 32 | public String toString() { 33 | return "Player: " + this.playerName + ", " + "goals " + this.playerGoals; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /week_4/079 NumberStatistics.java: -------------------------------------------------------------------------------- 1 | 2 | public class NumberStatistics { 3 | private int amountOfNumbers; 4 | private int sum; 5 | 6 | public NumberStatistics() { 7 | this.amountOfNumbers = 0; 8 | this.sum = 0; 9 | } 10 | 11 | public void addNumber (int number) { 12 | this.amountOfNumbers++; 13 | this.sum += number; 14 | } 15 | 16 | public int amountOfNumbers() { 17 | return this.amountOfNumbers; 18 | } 19 | 20 | public int sum() { 21 | if (amountOfNumbers == 0) { 22 | return 0; 23 | } 24 | else { 25 | return this.sum; 26 | } 27 | } 28 | 29 | public double average() { 30 | if (amountOfNumbers == 0) { 31 | return 0; 32 | } 33 | else { 34 | return this.sum*1.0/this.amountOfNumbers; 35 | } 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /week_6/106 BinarySearch.java: -------------------------------------------------------------------------------- 1 | public class BinarySearch { 2 | public static boolean search(int[] array, int searchedValue) { 3 | int beginning = 0; 4 | int end = array.length - 1; 5 | int middle = (beginning + end) / 2; 6 | 7 | while (beginning <= end) { 8 | middle = (beginning + end) / 2; 9 | if (array[middle] == searchedValue) { 10 | //System.out.println("Value " + searchedValue + " in array"); 11 | return true; 12 | } 13 | if (array[middle] > searchedValue) { 14 | end = middle - 1; 15 | } 16 | else if (array[middle] < searchedValue) { 17 | beginning = middle +1; 18 | } 19 | 20 | 21 | // restrict the search area 22 | } 23 | //System.out.println("Value " + searchedValue + " is not in array"); 24 | return false; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /week_5/092 Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | MyDate first = new MyDate(24, 12, 2009); 4 | MyDate second = new MyDate(1, 1, 2011); 5 | MyDate third = new MyDate(25, 12, 2010); 6 | 7 | System.out.println( first + " and " + second + " difference in years: " + second.differenceInYears(first) ); 8 | System.out.println( second + " and " + first + " difference in years: " + first.differenceInYears(second) ); 9 | System.out.println( first + " and " + third + " difference in years: " + third.differenceInYears(first) ); 10 | System.out.println( third + " and " + first + " difference in years: " + first.differenceInYears(third) ); 11 | System.out.println( third + " and " + second + " difference in years: " + second.differenceInYears(third) ); 12 | System.out.println( second + " and " + third + " difference in years: " + third.differenceInYears(second) ); 13 | } 14 | } -------------------------------------------------------------------------------- /week_6/098 Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | int[] original = {1, 2, 3, 4}; 7 | int[] reverse = reverseCopy(original); 8 | 9 | // print both 10 | System.out.println( "original: " +Arrays.toString(original)); 11 | System.out.println( "reversed: " +Arrays.toString(reverse)); 12 | } 13 | 14 | public static int[] copy(int[] array) { 15 | int[] newCopy = new int[array.length]; 16 | for (int i=0; i=0;i--) { 26 | reverseCopy[i] = array[j]; 27 | j++; 28 | } 29 | return reverseCopy; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /week_5/087 Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // write testcode here 6 | Apartment studioManhattan = new Apartment(1, 16, 5500); 7 | Apartment twoRoomsBrooklyn = new Apartment(2, 38, 4200); 8 | Apartment fourAndKitchenBronx = new Apartment(3, 78, 2500); 9 | 10 | 11 | System.out.println( studioManhattan.larger(twoRoomsBrooklyn) ); // false 12 | System.out.println( fourAndKitchenBronx.larger(twoRoomsBrooklyn) ); // true 13 | 14 | System.out.println( studioManhattan.priceDifference(twoRoomsBrooklyn) ); // 71600 15 | System.out.println( fourAndKitchenBronx.priceDifference(twoRoomsBrooklyn) ); // 35400 16 | 17 | System.out.println( studioManhattan.moreExpensiveThan(twoRoomsBrooklyn) ); // false 18 | System.out.println( fourAndKitchenBronx.moreExpensiveThan(twoRoomsBrooklyn) ); // true 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /week_3/069 Palindromi.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Palindromi { 4 | 5 | public static boolean palindrome(String text) { 6 | String textBackwards = ""; 7 | int i=(text.length()-1); 8 | while (i>=0) { 9 | char letter = text.charAt(i); 10 | i--; 11 | textBackwards = textBackwards + letter; 12 | } 13 | if (text.equals(textBackwards)) { 14 | return true; 15 | } 16 | else { 17 | return false; 18 | } 19 | } 20 | 21 | 22 | public static void main(String[] args) { 23 | Scanner reader = new Scanner(System.in); 24 | 25 | System.out.println("Type a text: "); 26 | String text = reader.nextLine(); 27 | if (palindrome(text)) { 28 | System.out.println("The text is a palindrome!"); 29 | } else { 30 | System.out.println("The text is not a palindrome!"); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /week_1/013 Main.java: -------------------------------------------------------------------------------- 1 | 2 | import nhlstats.NHLStatistics; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | System.out.println("Top ten players based on goals"); 8 | NHLStatistics.sortByGoals(); 9 | NHLStatistics.top(10); 10 | System.out.println(""); 11 | System.out.println("Top 25 players based on penalty amounts"); 12 | NHLStatistics.sortByPenalties(); 13 | NHLStatistics.top(25); 14 | System.out.println(""); 15 | System.out.println("Statistics for Sidney Crosby"); 16 | NHLStatistics.searchByPlayer("Sidney Crosby"); 17 | System.out.println(""); 18 | System.out.println("Statistics for Philadelphia Flyers"); 19 | NHLStatistics.teamStatistics("PHI"); 20 | System.out.println(""); 21 | System.out.println("Players in Anaheim Ducks ordered by points"); 22 | NHLStatistics.sortByPoints(); 23 | NHLStatistics.teamStatistics("ANA"); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /week_2/041 GuessingNumberGame.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Random; 3 | import java.util.Scanner; 4 | 5 | public class GuessingNumberGame { 6 | 7 | public static void main(String[] args) { 8 | Scanner reader = new Scanner(System.in); 9 | int number = drawNumber(); 10 | int i=0; 11 | while (true) { 12 | System.out.print("Guess a number: "); 13 | int guess = Integer.parseInt(reader.nextLine()); 14 | i++; 15 | if (guess == number) { 16 | break;} 17 | if (guess < number) { 18 | System.out.println("The number is greater, guesses made: " + i);} 19 | else { 20 | System.out.println("The number is lesser, guesses made: " + i);} 21 | } 22 | System.out.println("Congratulations, your guess is correct!"); 23 | } 24 | 25 | // DO NOT MODIFY THIS! 26 | private static int drawNumber() { 27 | return new Random().nextInt(101); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /week_4/070 CombiningArrayLists.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | import java.util.Collections; 4 | 5 | public class CombiningArrayLists { 6 | 7 | public static void main(String[] args) { 8 | 9 | // You can change the values while testing 10 | ArrayList list1 = new ArrayList(); 11 | ArrayList list2 = new ArrayList(); 12 | 13 | Collections.addAll(list1, 4, 3); 14 | // A shorthand for: 15 | // list1.add(4); 16 | // list1.add(3); 17 | 18 | 19 | Collections.addAll(list2, 5, 10, 7); 20 | // A shorthand for: 21 | // list2.add(5); 22 | // list2.add(10); 23 | // list2.add(7); 24 | 25 | // Implement the following method and then remove the comment 26 | combine(list1, list2); 27 | System.out.println(list1); 28 | System.out.println(list2); 29 | 30 | 31 | } 32 | 33 | public static void combine(ArrayList list1, ArrayList list2){ 34 | list1.addAll(list2); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /week_6/094 Phonebook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | import java.util.ArrayList; 12 | 13 | public class Phonebook { 14 | private ArrayList persons; 15 | 16 | public Phonebook() { 17 | this.persons = new ArrayList(); 18 | } 19 | 20 | public void add(String name, String number) { 21 | Person personToAdd = new Person(name, number); 22 | persons.add(personToAdd); 23 | } 24 | 25 | public void printAll() { 26 | for (Person i : persons) { 27 | System.out.println(i); 28 | } 29 | } 30 | 31 | public String searchNumber(String name) { 32 | for (Person i : persons) { 33 | if (i.getName().equals(name)) { 34 | return i.getNumber(); 35 | } 36 | } 37 | return "number not known"; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /week_6/103 Main.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | 8 | // implement your program here 9 | // do not put all to one method/class but rather design a proper structure to your program 10 | 11 | // Your program should use only one Scanner object, i.e., it is allowed to call 12 | // new Scanner only once. If you need scanner in multiple places, you can pass it as parameter 13 | Scanner reader = new Scanner(System.in); 14 | Commands command = new Commands(); 15 | command.menu(reader); 16 | //command.add(new Bird("Teal","Common")); 17 | //command.add(new Bird("Robbin","Christmas Bird")); 18 | //command.add(new Bird("Numchuck","Ninja Bird")); 19 | //command.observe("Teal"); 20 | //command.observe("Robbin"); 21 | //command.observe("Robbin"); 22 | //command.observe("Numchuck"); 23 | //command.statistics(); 24 | //command.show("Teal"); 25 | //command.show("Numchuck"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /week_4/082 LotteryNumbers.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Random; 3 | 4 | public class LotteryNumbers { 5 | private ArrayList numbers; 6 | 7 | public LotteryNumbers() { 8 | // Draw numbers as LotteryNumbers is created 9 | this.drawNumbers(); 10 | } 11 | 12 | public ArrayList numbers() { 13 | return this.numbers; 14 | } 15 | 16 | public void drawNumbers() { 17 | // We'll format a list for the numbers 18 | this.numbers = new ArrayList(); 19 | Random randomiser = new Random(); 20 | int i=0; 21 | while (i<7) { 22 | int number = randomiser.nextInt(39)+1; 23 | if (!this.numbers.contains(number)) { 24 | numbers.add(number); 25 | i++; 26 | } 27 | } 28 | } 29 | 30 | 31 | public boolean containsNumber(int number) { 32 | if (this.numbers.contains(number)) { 33 | return true; 34 | } 35 | else { 36 | return false;} 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /week_5/089 BoundedCounter.java: -------------------------------------------------------------------------------- 1 | 2 | // define attributes 3 | public class BoundedCounter { 4 | private int value; 5 | private int upperLimit; 6 | 7 | // constuctor 8 | public BoundedCounter (int upperLimit) { 9 | this.value=0; 10 | this.upperLimit=upperLimit; 11 | } 12 | 13 | // method next 14 | public void next() { 15 | if (this.value + 1 > upperLimit) { 16 | this.value=0; 17 | } 18 | else { 19 | this.value++; 20 | } 21 | } 22 | 23 | // method get value 24 | public int getValue() { 25 | return this.value; 26 | } 27 | 28 | // set value 29 | 30 | public void setValue(int value) { 31 | if (value < 0 || value > upperLimit) { 32 | } 33 | else { 34 | this.value=value; 35 | } 36 | } 37 | 38 | // method to string 39 | 40 | public String toString() { 41 | if (this.value < 10) { 42 | return "0" + this.value; 43 | } 44 | else { 45 | return "" + this.value; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /week_5/088 Main.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Scanner reader = new Scanner(System.in); 9 | ArrayList list = new ArrayList(); 10 | while (true) { 11 | System.out.print("name: "); 12 | String name = reader.nextLine(); 13 | if (name.equals("")) { 14 | break; 15 | } 16 | System.out.print("studentnumber: "); 17 | String studentNumber = reader.nextLine(); 18 | Student student = new Student(name,studentNumber); 19 | list.add(student); 20 | } 21 | for (Student i : list) { 22 | System.out.println(i); 23 | } 24 | 25 | System.out.println(""); 26 | System.out.print("Give search term: "); 27 | String search = reader.nextLine(); 28 | System.out.println("Result:"); 29 | for (Student i : list) { 30 | if (i.getName().contains(search)) { 31 | System.out.println(i); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /week_5/087 Apartment.java: -------------------------------------------------------------------------------- 1 | 2 | public class Apartment { 3 | 4 | private int rooms; 5 | private int squareMeters; 6 | private int pricePerSquareMeter; 7 | 8 | public Apartment(int rooms, int squareMeters, int pricePerSquareMeter) { 9 | this.rooms = rooms; 10 | this.squareMeters = squareMeters; 11 | this.pricePerSquareMeter = pricePerSquareMeter; 12 | } 13 | 14 | public boolean larger(Apartment otherApartment) { 15 | if (this.squareMeters>otherApartment.squareMeters) { 16 | return true; 17 | } 18 | return false; 19 | } 20 | 21 | public int priceDifference(Apartment otherApartment) { 22 | return Math.abs((this.pricePerSquareMeter*this.squareMeters)-(otherApartment.pricePerSquareMeter*otherApartment.squareMeters)); 23 | } 24 | 25 | public boolean moreExpensiveThan(Apartment otherApartment) { 26 | if ((this.pricePerSquareMeter*this.squareMeters)>(otherApartment.pricePerSquareMeter*otherApartment.squareMeters)) { 27 | return true; 28 | } 29 | return false; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /week_3/068 MoreThanOnce.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class MoreThanOnce { 5 | 6 | public static boolean moreThanOnce(ArrayList list, int searched) { 7 | int i=0; 8 | int j=(list.size()-1); 9 | while (j>=0) { 10 | if(list.get(j)==searched) { 11 | i++;} 12 | j--; 13 | } 14 | boolean Repeat = false; 15 | if (i>1) { 16 | Repeat = true;} 17 | return Repeat; 18 | } 19 | 20 | public static void main(String[] args) { 21 | Scanner reader = new Scanner(System.in); 22 | ArrayList list = new ArrayList(); 23 | list.add(1); 24 | list.add(2); 25 | list.add(7); 26 | list.add(2); 27 | 28 | System.out.println("Type a number: "); 29 | int number = Integer.parseInt(reader.nextLine()); 30 | if (moreThanOnce(list, number)) { 31 | System.out.println(number + " appears more than once."); 32 | } else { 33 | System.out.println(number + " does not appear more than once. "); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /week_3/067 Variance.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Variance { 4 | // Copy here sum from exercise 63 5 | public static int sum(ArrayList list) { 6 | int total = 0; 7 | for (int number : list) { 8 | total += number; 9 | } 10 | return total; 11 | } 12 | 13 | // Copy here average from exercise 64 14 | public static double average(ArrayList list) { 15 | double average = sum(list)/(list.size()*1.0); 16 | return average; 17 | } 18 | 19 | public static double variance(ArrayList list) { 20 | double var1 = 0; 21 | double avg1 = average(list); 22 | int n = list.size(); 23 | for (int number : list) { 24 | var1 += Math.pow(number - avg1,2); 25 | } 26 | return (var1/(n-1)); 27 | } 28 | 29 | public static void main(String[] args) { 30 | ArrayList list = new ArrayList(); 31 | list.add(3); 32 | list.add(2); 33 | list.add(7); 34 | list.add(2); 35 | 36 | System.out.println("The variance is: " + variance(list)); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /week_5/089 Clock.java: -------------------------------------------------------------------------------- 1 | public class Clock { 2 | private BoundedCounter hours; 3 | private BoundedCounter minutes; 4 | private BoundedCounter seconds; 5 | 6 | public Clock(int hoursAtBeginning, int minutesAtBeginning, int secondsAtBeginning) { 7 | this.hours = new BoundedCounter(23); 8 | this.minutes = new BoundedCounter(59); 9 | this.seconds = new BoundedCounter(59); 10 | hours.setValue(hoursAtBeginning); 11 | minutes.setValue(minutesAtBeginning); 12 | seconds.setValue(secondsAtBeginning); 13 | // the counters that represent hours, minutes and seconds are created and set to have the correct initial values 14 | } 15 | 16 | public void tick() { 17 | // Clock advances by one second 18 | seconds.next(); 19 | if (seconds.getValue()==0) { 20 | minutes.next(); 21 | } 22 | if (minutes.getValue()==0 && seconds.getValue()==0) { 23 | hours.next(); 24 | } 25 | } 26 | 27 | public String toString() { 28 | // returns the string representation 29 | return this.hours + ":" + this.minutes + ":" + this.seconds; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /week_2/036 LoopsEndingRemembering.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class LoopsEndingRemembering { 4 | public static void main(String[] args) { 5 | // program in this project exercises 36.1-36.5 6 | // actually this is just one program that is split in many parts 7 | 8 | Scanner reader = new Scanner(System.in); 9 | System.out.println("Type numbers: "); 10 | int total = 0; 11 | int i = 0; 12 | int even = 0; 13 | int odd = 0; 14 | while (true) { 15 | int number = Integer.parseInt(reader.nextLine()); 16 | if (number == -1) { 17 | break;} 18 | total += number; 19 | i++; 20 | if (number % 2 == 0) { 21 | even++;} 22 | else { 23 | odd++;} 24 | } 25 | System.out.println("Thank you and see you later!"); 26 | System.out.println("The sum is " + total); 27 | System.out.println("How many numbers: " + i); 28 | double mean = (double)total/i; 29 | System.out.println("Average: " + mean); 30 | System.out.println("Even numbers: " + even); 31 | System.out.println("Odd numbers: " + odd); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /week_1/023 Graph.java: -------------------------------------------------------------------------------- 1 | import org.jfree.chart.*; 2 | import org.jfree.chart.axis.*; 3 | import org.jfree.chart.plot.*; 4 | import org.jfree.chart.renderer.xy.*; 5 | import org.jfree.data.xy.*; 6 | 7 | /** DO NOT MODIFY THIS FILE */ 8 | 9 | public class Graph { 10 | 11 | private ChartFrame frame; 12 | private JFreeChart chart; 13 | private Plot plot; 14 | private XYDataset data; 15 | private XYSeries series; 16 | 17 | public Graph(String nimi) { 18 | series = new XYSeries(nimi); 19 | series.add(0,0); 20 | data = new XYSeriesCollection(series); 21 | plot = new XYPlot(data, new NumberAxis("x"), new NumberAxis("y"), new SamplingXYLineRenderer()); 22 | chart = new JFreeChart(plot); 23 | frame = new ChartFrame(nimi,chart); 24 | frame.pack(); 25 | frame.setVisible(true); 26 | } 27 | 28 | public static Graph instance; 29 | 30 | public static Graph getInstance() { 31 | if (instance == null) 32 | instance = new Graph("Graph"); 33 | return instance; 34 | } 35 | 36 | public static void addNumber(double y) { 37 | getInstance().lisaaNumero_(y); 38 | } 39 | 40 | public void lisaaNumero_(double y) { 41 | double x = series.getMaxX()+1; 42 | series.add(x,y); 43 | } 44 | } -------------------------------------------------------------------------------- /week_4/077 LyyraCard.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | 12 | // define attributes 13 | public class LyyraCard { 14 | private double balance; 15 | 16 | // constructer 17 | public LyyraCard(double balanceAtStart) { 18 | this.balance = balanceAtStart; 19 | } 20 | 21 | // to string method 22 | 23 | public String toString() { 24 | return "The card has " + this.balance + " euros"; 25 | } 26 | 27 | // pay economical method 28 | 29 | public void payEconomical() { 30 | if (this.balance >= 2.5) { 31 | this.balance -= 2.5; 32 | } 33 | } 34 | 35 | // pay gourmet method 36 | 37 | public void payGourmet() { 38 | if (this.balance >= 4.0) { 39 | this.balance -= 4.0; 40 | } 41 | } 42 | 43 | // load money 44 | 45 | public void loadMoney(double amount) { 46 | if (amount < 0) { 47 | } 48 | else if (this.balance + amount > 150) { 49 | this.balance = 150; 50 | } 51 | else { 52 | this.balance += amount; 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /week_4/078 BoundedCounter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | // define attributes 12 | public class BoundedCounter { 13 | private int value; 14 | private int upperLimit; 15 | 16 | // constuctor 17 | public BoundedCounter (int upperLimit) { 18 | this.value=0; 19 | this.upperLimit=upperLimit; 20 | } 21 | 22 | // method next 23 | public void next() { 24 | if (this.value + 1 > upperLimit) { 25 | this.value=0; 26 | } 27 | else { 28 | this.value++; 29 | } 30 | } 31 | 32 | // method get value 33 | public int getValue() { 34 | return this.value; 35 | } 36 | 37 | // set value 38 | 39 | public void setValue(int value) { 40 | if (value < 0 || value > upperLimit) { 41 | } 42 | else { 43 | this.value=value; 44 | } 45 | } 46 | 47 | // method to string 48 | 49 | public String toString() { 50 | if (this.value < 10) { 51 | return "0" + this.value; 52 | } 53 | else { 54 | return "" + this.value; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /week_5/085 Person.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO TOT TOUCH THIS CLASS! 3 | */ 4 | 5 | public class Person { 6 | 7 | private String name; 8 | private int age; 9 | private int height; 10 | private int weight; 11 | 12 | public Person(String name, int age, int height, int weight) { 13 | this.name = name; 14 | this.age = age; 15 | this.height = height; 16 | this.weight = weight; 17 | } 18 | 19 | public void becomeOlder() { 20 | this.age++; 21 | } 22 | 23 | public boolean adult() { 24 | if (this.age < 18) { 25 | return false; 26 | } 27 | 28 | return true; 29 | } 30 | 31 | public double weightIndex() { 32 | double heightInMeters = this.height / 100.0; 33 | 34 | return this.weight / (heightInMeters * heightInMeters); 35 | } 36 | 37 | public void setHeight(int height) { 38 | this.height = height; 39 | } 40 | 41 | public int getHeight() { 42 | return this.height; 43 | } 44 | 45 | public int getWeight() { 46 | return this.weight; 47 | } 48 | 49 | public void setWeight(int weight) { 50 | this.weight = weight; 51 | } 52 | 53 | public String getName() { 54 | return this.name; 55 | } 56 | 57 | public String toString() { 58 | return this.name + " I am " + this.age + " years old, my weight index is " + this.weightIndex(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /week_5/090 Team.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | import java.util.ArrayList; 12 | 13 | public class Team { 14 | private String name; 15 | private ArrayList players; 16 | private int upperBound; 17 | 18 | public Team(String name) { 19 | this.name = name; 20 | this.players = new ArrayList(); 21 | this.upperBound = 16; 22 | } 23 | 24 | public String getName() { 25 | return this.name; 26 | } 27 | 28 | public void addPlayer(Player player) { 29 | if (players.size() < upperBound) { 30 | this.players.add(player); 31 | } 32 | } 33 | 34 | public int goals() { 35 | int goals = 0; 36 | for (Player player: this.players) { 37 | goals += player.goals(); 38 | } 39 | return goals; 40 | } 41 | 42 | public void setMaxSize(int maxSize) { 43 | this.upperBound = maxSize; 44 | } 45 | 46 | public int size() { 47 | return this.players.size(); 48 | } 49 | 50 | public void printPlayers() { 51 | for (Player player : this.players) { 52 | System.out.println(player); 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /week_5/093 Person.java: -------------------------------------------------------------------------------- 1 | import java.util.Calendar; 2 | 3 | public class Person { 4 | private String name; 5 | private MyDate birthday; 6 | 7 | public Person(String name, int pp, int kk, int vv) { 8 | this.name = name; 9 | this.birthday = new MyDate(pp, kk, vv); 10 | } 11 | 12 | public Person(String name, MyDate birthday) { 13 | this.name=name; 14 | this.birthday=birthday; 15 | } 16 | 17 | public Person(String name) { 18 | this(name,Calendar.getInstance().get(Calendar.DATE),Calendar.getInstance().get(Calendar.MONTH) + 1,Calendar.getInstance().get(Calendar.YEAR)); 19 | } 20 | 21 | 22 | public int age() { 23 | int currentDay = Calendar.getInstance().get(Calendar.DATE); 24 | int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1; 25 | int currentYear = Calendar.getInstance().get(Calendar.YEAR); 26 | MyDate currentDate = new MyDate(currentDay, currentMonth, currentYear); 27 | return birthday.differenceInYears(currentDate); 28 | } 29 | 30 | 31 | public String getName() { 32 | return this.name; 33 | } 34 | 35 | public String toString() { 36 | return this.name + ", born " + this.birthday; 37 | } 38 | 39 | public boolean olderThan(Person compared) { 40 | if (this.birthday.earlier(compared.birthday)) { 41 | return true; 42 | } 43 | return false; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /week_4/079 Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner reader = new Scanner(System.in); 6 | NumberStatistics stats = new NumberStatistics(); 7 | NumberStatistics even = new NumberStatistics(); 8 | NumberStatistics odd = new NumberStatistics(); 9 | 10 | System.out.println("Type numbers: "); 11 | 12 | while (true) { 13 | int read = Integer.parseInt(reader.nextLine()); 14 | if (read==-1) { 15 | break; 16 | } 17 | stats.addNumber(read); 18 | if (read%2==0) { 19 | even.addNumber(read); 20 | } 21 | else { 22 | odd.addNumber(read); 23 | } 24 | } 25 | 26 | System.out.println("sum: " + stats.sum()); 27 | System.out.println("sum of even: " + even.sum()); 28 | System.out.println("sum of odd: " + odd.sum()); 29 | 30 | 31 | 32 | 33 | 34 | //System.out.println("Amount: " + stats.amountOfNumbers()); 35 | // Write test code here 36 | // Remember to remove all the extra code when doing assignments 79.3 and 79.4 37 | 38 | // Define three NumberStatistics objects in your program: 39 | // The first is used to track the sum of all given numbers. 40 | // The second takes care of even numbers and the third the odd numbers. 41 | 42 | // The tests does not work if you do not create the objects in the correct order 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /week_2/040 PrintingLikeBoss.java: -------------------------------------------------------------------------------- 1 | public class PrintingLikeBoss { 2 | 3 | // copy or rewrite the method of Assignment 39.1 here 4 | public static void printStars(int amount) { 5 | int j=0; 6 | while (j < amount) { 7 | System.out.print("*"); 8 | j++; 9 | } 10 | System.out.println(""); 11 | } 12 | 13 | public static void printWhitespaces(int amount) { 14 | int i=0; 15 | while (i < amount) { 16 | System.out.print(" "); 17 | i++; 18 | } 19 | } 20 | 21 | public static void printTriangle(int size) { 22 | int i=0; 23 | int stars=0; 24 | int white=0; 25 | while (i 99) { 10 | euros += cents / 100; 11 | cents %= 100; 12 | } 13 | 14 | this.euros = euros; 15 | this.cents = cents; 16 | } 17 | 18 | public int euros() { 19 | return euros; 20 | } 21 | 22 | public int cents() { 23 | return cents; 24 | } 25 | 26 | public Money plus(Money added) { 27 | Money newMoney = new Money(this.euros + added.euros ,this.cents + added.cents); 28 | return newMoney; 29 | } 30 | 31 | public Money minus(Money decremented) { 32 | if (this.less(decremented)) { 33 | return new Money(0,0); 34 | } 35 | if (this.cents < decremented.cents) { 36 | int newEuros = this.euros - 1 - decremented.euros; 37 | int newCents = 100 - decremented.cents + this.cents; 38 | return new Money(newEuros,newCents); 39 | } 40 | return new Money(this.euros-decremented.euros,this.cents-decremented.cents); 41 | } 42 | 43 | public boolean less(Money compared) { 44 | if(this.euros < compared.euros | (this.euros == compared.euros && this.cents < compared.cents)) { 45 | return true; 46 | } 47 | return false; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | String zero = ""; 53 | if (cents < 10) { 54 | zero = "0"; 55 | } 56 | 57 | return euros + "." + zero + cents + "e"; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /week_5/091 MyDate.java: -------------------------------------------------------------------------------- 1 | 2 | public class MyDate { 3 | 4 | private int day; 5 | private int month; 6 | private int year; 7 | 8 | public MyDate(int day, int month, int year) { 9 | this.day = day; 10 | this.month = month; 11 | this.year = year; 12 | } 13 | 14 | public String toString() { 15 | return this.day + "." + this.month + "." + this.year; 16 | } 17 | 18 | public boolean earlier(MyDate compared) { 19 | if (this.year < compared.year) { 20 | return true; 21 | } 22 | 23 | if (this.year == compared.year && this.month < compared.month) { 24 | return true; 25 | } 26 | 27 | if (this.year == compared.year && this.month == compared.month 28 | && this.day < compared.day) { 29 | return true; 30 | } 31 | 32 | return false; 33 | } 34 | 35 | public void advance() { 36 | if (this.day <= 29) { 37 | this.day++; 38 | } 39 | else { 40 | this.day = 1; 41 | if (this.month <= 11) { 42 | this.month++; 43 | } 44 | else { 45 | this.month=1; 46 | this.year++; 47 | } 48 | } 49 | } 50 | 51 | public void advance(int numberOfDays) { 52 | int i = numberOfDays; 53 | while (i > 0) { 54 | this.advance(); 55 | i--; 56 | } 57 | } 58 | 59 | public MyDate afterNumberOfDays(int days) { 60 | MyDate newMyDate = new MyDate(this.day, this.month, this.year); 61 | newMyDate.advance(days); 62 | return newMyDate; 63 | } 64 | } -------------------------------------------------------------------------------- /week_6/100 NightSky.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | import java.util.Random; 12 | 13 | public class NightSky { 14 | private double density; 15 | private int width; 16 | private int height; 17 | private int starsInLastPrint; 18 | 19 | public NightSky(double density, int width, int height) { 20 | this.density=density; 21 | this.width=width; 22 | this.height=height; 23 | this.starsInLastPrint=0; 24 | } 25 | 26 | public NightSky(int width, int height) { 27 | this(0.1,width,height); 28 | } 29 | 30 | public NightSky(double density) { 31 | this(density, 20, 10); 32 | } 33 | 34 | public void printLine() { 35 | Random randomiser = new Random(); 36 | String line = ""; 37 | for (int i=0; i < this.width; i++) { 38 | double number = randomiser.nextDouble(); 39 | if (number <= this.density) { 40 | line+="*"; 41 | this.starsInLastPrint++; 42 | } 43 | else { 44 | line+=" "; 45 | } 46 | } 47 | System.out.println(line); 48 | } 49 | 50 | public void print() { 51 | for (int j=0; j < this.height; j++) { 52 | printLine(); 53 | } 54 | } 55 | 56 | public int starsInLastPrint() { 57 | int stars = this.starsInLastPrint; 58 | this.starsInLastPrint=0; 59 | return stars; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /week_2/039 Printing.java: -------------------------------------------------------------------------------- 1 | public class Printing { 2 | 3 | public static void printStars(int amount) { 4 | int i = 0; 5 | while (i < amount) { 6 | System.out.print("*"); 7 | i++; 8 | } 9 | System.out.println(""); 10 | // 39.1 11 | // you can print one star with the command 12 | // System.out.print("*"); 13 | // call this command amount times 14 | } 15 | 16 | public static void printSquare(int sideSize) { 17 | int i = 0; 18 | while (i < sideSize) { 19 | printStars(sideSize); 20 | i++; 21 | } 22 | // 39.2 23 | } 24 | 25 | public static void printRectangle(int width, int height) { 26 | int i = 0; 27 | while (i < height) { 28 | printStars(width); 29 | i++; 30 | } 31 | } 32 | 33 | public static void printTriangle(int size) { 34 | int i = 0; 35 | while (i < size) { 36 | i++; 37 | printStars(i); 38 | } 39 | } 40 | 41 | public static void main(String[] args) { 42 | // Tests do not use main, yo can write code here freely! 43 | // if you have problems with tests, please try out first 44 | // here to see that the printout looks correct 45 | 46 | printStars(5); 47 | printStars(3); 48 | printStars(9); 49 | System.out.println("\n---"); // printing --- to separate the figures 50 | printSquare(4); 51 | System.out.println("\n---"); 52 | printRectangle(17, 3); 53 | System.out.println("\n---"); 54 | printTriangle(4); 55 | System.out.println("\n---"); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /week_6/101 Library.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | 12 | import java.util.ArrayList; 13 | 14 | public class Library { 15 | private ArrayList books; 16 | 17 | public Library() { 18 | this.books = new ArrayList(); 19 | 20 | } 21 | 22 | public void addBook(Book newBook) { 23 | this.books.add(newBook); 24 | } 25 | 26 | public void printBooks() { 27 | for (Book i : this.books) { 28 | System.out.println(i); 29 | } 30 | } 31 | 32 | public ArrayList searchByTitle(String title) { 33 | ArrayList found = new ArrayList(); 34 | for (Book i : this.books) { 35 | if (StringUtils.included(i.title(),title)) { 36 | found.add(i); 37 | } 38 | } 39 | return found; 40 | } 41 | 42 | public ArrayList searchByPublisher(String publisher) { 43 | ArrayList found = new ArrayList(); 44 | for (Book i : this.books) { 45 | if (StringUtils.included(i.publisher(),publisher)) { 46 | found.add(i); 47 | } 48 | } 49 | return found; 50 | } 51 | 52 | public ArrayList searchByYear(int year) { 53 | ArrayList found = new ArrayList(); 54 | for (Book i : this.books) { 55 | if (i.year()==year) { 56 | found.add(i); 57 | } 58 | } 59 | return found; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /week_4/077 Main.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | LyyraCard cardPekka = new LyyraCard(20); 8 | LyyraCard cardBrian = new LyyraCard(30); 9 | 10 | cardPekka.payGourmet(); 11 | cardBrian.payEconomical(); 12 | 13 | System.out.println("Pekka: " + cardPekka.toString()); 14 | System.out.println("Brian: " + cardBrian.toString()); 15 | 16 | cardPekka.loadMoney(20); 17 | cardBrian.payGourmet(); 18 | 19 | System.out.println("Pekka: " + cardPekka.toString()); 20 | System.out.println("Brian: " + cardBrian.toString()); 21 | 22 | cardPekka.payEconomical(); 23 | cardPekka.payEconomical(); 24 | 25 | System.out.println("Pekka: " + cardPekka.toString()); 26 | 27 | cardBrian.loadMoney(50); 28 | 29 | System.out.println("Brian: " + cardBrian.toString()); 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | //card.loadMoney(-15); 38 | //System.out.println(card); 39 | 40 | //card.loadMoney(10); 41 | //System.out.println(card); 42 | 43 | //card.loadMoney(200); 44 | //System.out.println(card); 45 | 46 | //card.payEconomical(); 47 | //System.out.println(card); 48 | 49 | //card.payGourmet(); 50 | //System.out.println(card); 51 | //.payEconomical(); 52 | //card.payGourmet(); 53 | //System.out.println(card); 54 | // add here code that tests LyraCard. However before doing 77.6 remove the 55 | // other code 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /week_5/083 HangmanLogic.java: -------------------------------------------------------------------------------- 1 | 2 | public class HangmanLogic { 3 | 4 | private String word; 5 | private String guessedLetters; 6 | private int numberOfFaults; 7 | 8 | public HangmanLogic(String word) { 9 | this.word = word.toUpperCase(); 10 | this.guessedLetters = ""; 11 | this.numberOfFaults = 0; 12 | } 13 | 14 | public int numberOfFaults() { 15 | return this.numberOfFaults; 16 | } 17 | 18 | public String guessedLetters() { 19 | return this.guessedLetters; 20 | } 21 | 22 | public int losingFaultAmount() { 23 | return 12; 24 | } 25 | 26 | public void guessLetter(String letter) { 27 | if (guessedLetters.contains(letter)) { 28 | } 29 | else if (word.contains(letter)) { 30 | this.guessedLetters += letter; 31 | } 32 | else { 33 | this.numberOfFaults++; 34 | this.guessedLetters += letter; 35 | } 36 | } 37 | 38 | public String hiddenWord() { 39 | int i = 0; 40 | int j = (word.length()); 41 | String hideword=""; 42 | while (i < j) { 43 | String a = ""+word.charAt(i); 44 | if (guessedLetters.contains(a)) { 45 | hideword+=a; 46 | } 47 | else { 48 | hideword+="_"; 49 | } 50 | i++; 51 | } 52 | // program here the functionality for building the hidden word 53 | 54 | // create the hidden word by interating through this.word letter by letter 55 | // if the letter in turn is within the guessed words, put it in to the hidden word 56 | // if the letter is not among guessed, replace it with _ in the hidden word 57 | 58 | // return the hidden word at the end 59 | return hideword; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /week_5/092 MyDate.java: -------------------------------------------------------------------------------- 1 | public class MyDate { 2 | private int day; 3 | private int month; 4 | private int year; 5 | 6 | public MyDate(int day, int montd, int year) { 7 | this.day = day; 8 | this.month = montd; 9 | this.year = year; 10 | } 11 | 12 | public String toString() { 13 | return this.day + "." + this.month + "." + this.year; 14 | } 15 | 16 | public boolean earlier(MyDate compared) { 17 | if (this.year < compared.year) { 18 | return true; 19 | } 20 | 21 | if (this.year == compared.year && this.month < compared.month) { 22 | return true; 23 | } 24 | 25 | if (this.year == compared.year && this.month == compared.month 26 | && this.day < compared.day) { 27 | return true; 28 | } 29 | 30 | return false; 31 | } 32 | 33 | public int differenceInYears(MyDate comparedDate) { 34 | if (this.earlier(comparedDate)) { 35 | if (comparedDate.month > this.month) { 36 | return comparedDate.year - this.year; 37 | } 38 | else if (comparedDate.month == this.month && comparedDate.day >= this.day) { 39 | return comparedDate.year - this.year; 40 | } 41 | else { 42 | return comparedDate.year - this.year -1; 43 | } 44 | } 45 | else { 46 | if (this.month > comparedDate.month) { 47 | return this.year - comparedDate.year; 48 | } 49 | else if (comparedDate.month == this.month && this.day >= comparedDate.day) { 50 | return this.year - comparedDate.year; 51 | } 52 | else { 53 | return this.year - comparedDate.year -1; 54 | } 55 | } 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /week_5/093 MyDate.java: -------------------------------------------------------------------------------- 1 | public class MyDate { 2 | 3 | private int day; 4 | private int month; 5 | private int year; 6 | 7 | public MyDate(int pv, int kk, int vv) { 8 | this.day = pv; 9 | this.month = kk; 10 | this.year = vv; 11 | } 12 | 13 | public String toString() { 14 | return this.day + "." + this.month + "." + this.year; 15 | } 16 | 17 | public boolean earlier(MyDate compared) { 18 | if (this.year < compared.year) { 19 | return true; 20 | } 21 | 22 | if (this.year == compared.year && this.month < compared.month) { 23 | return true; 24 | } 25 | 26 | if (this.year == compared.year && this.month == compared.month 27 | && this.day < compared.day) { 28 | return true; 29 | } 30 | 31 | return false; 32 | } 33 | 34 | public int differenceInYears(MyDate comparedDate) { 35 | if (this.earlier(comparedDate)) { 36 | if (comparedDate.month > this.month) { 37 | return comparedDate.year - this.year; 38 | } 39 | else if (comparedDate.month == this.month && comparedDate.day >= this.day) { 40 | return comparedDate.year - this.year; 41 | } 42 | else { 43 | return comparedDate.year - this.year -1; 44 | } 45 | } 46 | else { 47 | if (this.month > comparedDate.month) { 48 | return this.year - comparedDate.year; 49 | } 50 | else if (comparedDate.month == this.month && this.day >= comparedDate.day) { 51 | return this.year - comparedDate.year; 52 | } 53 | else { 54 | return this.year - comparedDate.year -1; 55 | } 56 | } 57 | } 58 | 59 | /* 60 | * In assignment 92 method differneceInYears was added to MyDate 61 | * Copy the method here since it eases this assignment considerably. 62 | */ 63 | 64 | } -------------------------------------------------------------------------------- /week_4/078 Main.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws Exception { 7 | Scanner reader = new Scanner(System.in); 8 | BoundedCounter seconds = new BoundedCounter(59); 9 | BoundedCounter minutes = new BoundedCounter(59); 10 | BoundedCounter hours = new BoundedCounter(23); 11 | 12 | //System.out.print("seconds: "); 13 | //int s = Integer.parseInt(reader.nextLine()); 14 | //System.out.print("minutes: "); 15 | //int m = Integer.parseInt(reader.nextLine()); 16 | // System.out.print("hours: "); 17 | // int h = Integer.parseInt(reader.nextLine()); 18 | 19 | // seconds.setValue(s); 20 | //minutes.setValue(m); 21 | //hours.setValue(h); 22 | 23 | seconds.setValue(0); 24 | minutes.setValue(55); 25 | hours.setValue(9); 26 | 27 | //int i=0; 28 | //while (i<121) { 29 | // System.out.println(hours + ":" + minutes + ":" + seconds); 30 | // seconds.next(); 31 | // if (seconds.getValue()==0) { 32 | // minutes.next(); 33 | // } 34 | // if (minutes.getValue()==0 && seconds.getValue()==0) { 35 | // hours.next(); 36 | // } 37 | // i++; 38 | // } 39 | // write here code to ensure that BoundedCounter works as expected 40 | // before starting 78.3 remove the extra code and use the skeleton shown 41 | // in the assignment 42 | 43 | int i=0; 44 | while (true) { 45 | System.out.println(hours + ":" + minutes + ":" + seconds); 46 | Thread.sleep(1000); 47 | seconds.next(); 48 | if (seconds.getValue()==0) { 49 | minutes.next(); 50 | } 51 | if (minutes.getValue()==0 && seconds.getValue()==0) { 52 | hours.next(); 53 | } 54 | i++; 55 | 56 | } 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /week_5/084 Counter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | // define attributes 12 | public class Counter { 13 | private int value; 14 | private boolean check; 15 | 16 | // create constructor to be called 17 | public Counter(int startingValue, boolean check) { 18 | this.value = startingValue; 19 | this.check = check; 20 | } 21 | 22 | // constructor given starting value (check set to off) 23 | public Counter(int startingValue) { 24 | this(startingValue,false); 25 | } 26 | 27 | // constructor given check (starting value set to 0 28 | public Counter(boolean check) { 29 | this(0,check); 30 | } 31 | 32 | // constructor no values 33 | public Counter() { 34 | this(0,false); 35 | } 36 | 37 | // return current value of counter 38 | public int value() { 39 | return this.value; 40 | } 41 | // increase counter by number 42 | public void increase(int number) { 43 | if (number>0) { 44 | this.value+=number; 45 | } 46 | } 47 | 48 | // increase counter by 1 49 | public void increase() { 50 | this.value++; 51 | } 52 | 53 | // decrease counter by 1 54 | public void decrease() { 55 | if (value > 0 && this.check == true) { 56 | this.value--; 57 | } 58 | else if (this.check == false) { 59 | this.value--; 60 | } 61 | } 62 | 63 | // decrease counter by number 64 | public void decrease(int number) { 65 | if ((value - number) > 0 && this.check == true && number>0) { 66 | this.value-=number; 67 | } 68 | else if ((value - number) < 0 && this.check == true && number>0) { 69 | this.value=0; 70 | } 71 | else if (this.check == false && number>0) { 72 | this.value-=number; 73 | } 74 | } 75 | 76 | 77 | public String toString() { 78 | return value + " parameter: " + this.check; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /week_1/024 NhlStatisticsPart2.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | import nhlstats.NHLStatistics; 4 | 5 | public class NhlStatisticsPart2 { 6 | 7 | public static void main(String[] args) { 8 | Scanner reader = new Scanner(System.in); 9 | 10 | System.out.println("NHL statistics service"); 11 | 12 | while (true) { 13 | System.out.println(""); 14 | System.out.print("command (points, goals, assists, penalties, player, club, quit): "); 15 | String command = reader.nextLine(); 16 | 17 | if (command.equals("quit")) { 18 | break; 19 | } 20 | 21 | if (command.equals("points")) { 22 | // Print the top ten players sorted by points. 23 | NHLStatistics.sortByPoints(); 24 | NHLStatistics.top(10); 25 | } else if (command.equals("goals")) { 26 | // Print the top ten players sorted by goals. 27 | NHLStatistics.sortByGoals(); 28 | NHLStatistics.top(10); 29 | } else if (command.equals("assists")) { 30 | // Print the top ten players sorted by assists. 31 | NHLStatistics.sortByAssists(); 32 | NHLStatistics.top(10); 33 | } else if (command.equals("penalties")) { 34 | // Print the top ten players sorted by penalties. 35 | NHLStatistics.sortByPenalties(); 36 | NHLStatistics.top(10); 37 | } else if (command.equals("player")) { 38 | // Ask the user first which player's statistics are needed and then print them. 39 | System.out.print("Which players statistics are needed? "); 40 | String playerWanted = reader.nextLine(); 41 | NHLStatistics.searchByPlayer(playerWanted); 42 | } else if (command.equals("club")) { 43 | // Ask the user first which club's statistics are needed and then print them. 44 | // Note: When printing statistics they should be ordered by points (so the players with the most points come first). 45 | System.out.print("Which club's statistics are needed? "); 46 | String clubWanted = reader.nextLine(); 47 | NHLStatistics.sortByPoints(); 48 | NHLStatistics.teamStatistics(clubWanted); 49 | } 50 | 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /week_6/105 GuessingGame.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class GuessingGame { 4 | 5 | private Scanner reader; 6 | 7 | public GuessingGame() { 8 | // use only this scanner, othervise the tests do not work 9 | this.reader = new Scanner(System.in); 10 | } 11 | 12 | public void play(int lowerLimit, int upperLimit) { 13 | instructions(lowerLimit, upperLimit); 14 | //int i=1; 15 | //int max = howManyTimesHalvable(upperLimit - lowerLimit); 16 | int value = average(lowerLimit,upperLimit); 17 | 18 | while (true) { 19 | value=average(lowerLimit,upperLimit); 20 | if (lowerLimit==upperLimit) { 21 | System.out.println("The number you're thinking of is " + lowerLimit); 22 | break; 23 | } 24 | if (isGreaterThan(value)) { 25 | lowerLimit=value+1; 26 | } 27 | else { 28 | upperLimit=value; 29 | 30 | } 31 | } 32 | 33 | 34 | // write the guessing logic here 35 | 36 | } 37 | 38 | // implement here the methods isGreaterThan and average 39 | 40 | public void instructions(int lowerLimit, int upperLimit) { 41 | int maxQuestions = howManyTimesHalvable(upperLimit - lowerLimit); 42 | 43 | System.out.println("Think of a number between " + lowerLimit + "..." + upperLimit + "."); 44 | 45 | System.out.println("I promise you that I can guess the number you are thinking with " + maxQuestions + " questions."); 46 | System.out.println(""); 47 | System.out.println("Next I'll present you a series of questions. Answer them honestly."); 48 | System.out.println(""); 49 | } 50 | 51 | // a helper method: 52 | public static int howManyTimesHalvable(int number) { 53 | // we create a base two logarithm of the given value 54 | 55 | // Below we swap the base number to base two logarithms! 56 | return (int) (Math.log(number) / Math.log(2)) + 1; 57 | } 58 | 59 | public boolean isGreaterThan(int value) { 60 | System.out.println("Is Is your number greater than " + value + "? (y/n)"); 61 | String response = reader.nextLine(); 62 | if (response.equals("y")) { 63 | return true; 64 | } 65 | else { 66 | return false; 67 | } 68 | } 69 | 70 | public int average(int firstNumber, int secondNumber) { 71 | return (firstNumber+secondNumber)/2; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /week_6/104 Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | int[] values = {6, 5, -7, -8, 8, 1, 7, 11, 7, -5, 7}; 6 | System.out.println("smallest: " + smallest(values)); 7 | System.out.println("Index of the smallest: " + indexOfTheSmallest(values)); 8 | System.out.println(indexOfTheSmallestStartingFrom(values, 4)); 9 | System.out.println(Arrays.toString(values)); 10 | sort(values); 11 | System.out.println(Arrays.toString(values)); 12 | swap(values, 1, 0); 13 | System.out.println(Arrays.toString(values)); 14 | System.out.println("---"); 15 | sort(values); 16 | } 17 | 18 | public static int smallest(int[] array) { 19 | int i = array[0]; 20 | for (int j : array) { 21 | if (j < i) { 22 | i = j; 23 | } 24 | } 25 | return i; 26 | } 27 | 28 | public static void sort(int[] array) { 29 | System.out.println(Arrays.toString(array)); 30 | for (int i=0; i < array.length; i++) { 31 | int indexNextSmallest = indexOfTheSmallestStartingFrom(array,i); 32 | swap(array, i, indexNextSmallest); 33 | System.out.println(Arrays.toString(array)); 34 | } 35 | } 36 | 37 | public static void swap(int[] array, int index1, int index2) { 38 | int swap1 = array[index1]; 39 | int swap2 = array[index2]; 40 | array[index1] = swap2; 41 | array[index2] = swap1; 42 | // for (int i : array) { 43 | // if (i==index1) { 44 | // array[i] = array[index2]; 45 | //} 46 | //else if (i==index2) { 47 | // array[i] = array[index1]; 48 | // } 49 | // else { 50 | //} 51 | //} 52 | } 53 | 54 | public static int indexOfTheSmallestStartingFrom(int[] array, int index) { 55 | int i = array[index]; 56 | int k = index; 57 | for (int j = (index+1); j < array.length; j++) { 58 | if (array[j] < i) { 59 | i = array[j]; 60 | k = j; 61 | } 62 | } 63 | return k; 64 | } 65 | 66 | public static int indexOfTheSmallest(int[] array) { 67 | int i = array[0]; 68 | int k = 0; 69 | for (int j = 1; j < array.length; j++) { 70 | if (array[j] < i) { 71 | i = array[j]; 72 | k = j; 73 | } 74 | } 75 | return k; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /week_6/102 Grade.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | * 11 | * 12 | */ 13 | import java.util.Scanner; 14 | import java.util.ArrayList; 15 | 16 | public class Grade { 17 | private int grade0; 18 | private int grade1; 19 | private int grade2; 20 | private int grade3; 21 | private int grade4; 22 | private int grade5; 23 | ArrayList grades = new ArrayList(); 24 | 25 | public Grade() { 26 | this.grade0=0; 27 | this.grade1=0; 28 | this.grade2=0; 29 | this.grade3=0; 30 | this.grade4=0; 31 | this.grade5=0; 32 | } 33 | 34 | public void createGrade(Scanner reader) { 35 | System.out.println("Type exam scores, -1 completes:"); 36 | while(true) { 37 | int points = Integer.parseInt(reader.nextLine()); 38 | if (points == -1) { 39 | grades.add(grade0); 40 | grades.add(grade1); 41 | grades.add(grade2); 42 | grades.add(grade3); 43 | grades.add(grade4); 44 | grades.add(grade5); 45 | break; 46 | } 47 | else if (points < 0) { 48 | } 49 | else if (points < 30) { 50 | grade0++; 51 | } 52 | else if (points < 35) { 53 | grade1++; 54 | } 55 | else if (points < 40) { 56 | grade2++; 57 | } 58 | else if (points < 45) { 59 | grade3++; 60 | } 61 | else if (points < 50) { 62 | grade4++; 63 | } 64 | else if (points <= 60) { 65 | grade5++; 66 | } 67 | else { 68 | } 69 | } 70 | } 71 | 72 | public void printStars(int grade) { 73 | String stars = ""; 74 | for (int i=0; i < grade; i++) { 75 | stars += "*"; 76 | } 77 | System.out.println(stars); 78 | } 79 | 80 | public double acceptance() { 81 | int bottom=0; 82 | for (int i=0; i < grades.size(); i++) { 83 | bottom+=grades.get(i); 84 | } 85 | return ((bottom-grade0)*100.0)/bottom; 86 | } 87 | 88 | public void print() { 89 | System.out.println("Grade distribution:"); 90 | for (int i=5; i>=0; i--) { 91 | System.out.print(i + ": "); 92 | printStars(grades.get(i)); 93 | } 94 | System.out.println("Acceptance percentage: " + acceptance()); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /week_6/103 Commands.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author andyr 10 | */ 11 | 12 | import java.util.ArrayList; 13 | import java.util.Scanner; 14 | 15 | public class Commands { 16 | private ArrayList birds; 17 | 18 | public Commands() { 19 | this.birds = new ArrayList(); 20 | } 21 | 22 | public void menu(Scanner reader) { 23 | while(true) { 24 | System.out.print("? "); 25 | String command = reader.nextLine(); 26 | if (command.equals("Quit")) { 27 | break; 28 | } 29 | else if (command.equals("Add")) { 30 | System.out.print("Name: "); 31 | String birdName = reader.nextLine(); 32 | System.out.print("Latin name: "); 33 | String latinName = reader.nextLine(); 34 | Bird newBird = new Bird(birdName,latinName); 35 | add(newBird); 36 | } 37 | else if (command.equals("Observation")) { 38 | System.out.print("What was observed:? "); 39 | String birdObserved = reader.nextLine(); 40 | observe(birdObserved); 41 | } 42 | else if (command.equals("Statistics")) { 43 | statistics(); 44 | } 45 | else if (command.equals("Show")) { 46 | System.out.print("What? "); 47 | String birdShow = reader.nextLine(); 48 | show(birdShow); 49 | } 50 | } 51 | } 52 | 53 | 54 | 55 | 56 | public void add(Bird newBird) { 57 | this.birds.add(newBird); 58 | } 59 | 60 | public void observe(String birdName) { 61 | if (birdContains(birdName)) { 62 | for (Bird i : this.birds) { 63 | if (i.getName().contains(birdName)) { 64 | i.addObservation(); 65 | } 66 | } 67 | } 68 | else { 69 | System.out.println("Is not a bird!"); 70 | } 71 | } 72 | 73 | 74 | public boolean birdContains(String birdName) { 75 | for (Bird i : this.birds) { 76 | if (i.getName().contains(birdName)) { 77 | return true; 78 | } 79 | } 80 | return false; 81 | } 82 | 83 | public void statistics() { 84 | for (Bird i : this.birds) { 85 | System.out.println(i.getString()); 86 | } 87 | } 88 | 89 | public void show(String birdName) { 90 | if (birdContains(birdName)) { 91 | for (Bird i : this.birds) { 92 | if (i.getName().equals(birdName)) { 93 | System.out.println(i.getString()); 94 | } 95 | } 96 | } 97 | else { 98 | System.out.println("Is not a bird!"); 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /week_5/086 CashRegister.java: -------------------------------------------------------------------------------- 1 | 2 | public class CashRegister { 3 | 4 | private double cashInRegister; 5 | private int economicalSold; 6 | private int gourmetSold; 7 | 8 | public CashRegister() { 9 | this.cashInRegister=1000; 10 | } 11 | 12 | public double payEconomical(double cashGiven) { 13 | double priceEconomical=2.50; 14 | if (cashGiven >= priceEconomical) { 15 | this.cashInRegister+=priceEconomical; 16 | this.economicalSold++; 17 | return cashGiven - priceEconomical; 18 | } 19 | // price of the economical lunch is 2.50 euros 20 | // if the given cash is at least the price of the lunch: 21 | // the price of lunch is added to register 22 | // the amount of sold lunch is incremented by one 23 | // method returns cashGiven - lunch price 24 | // if not enough money given, all is returned and nothing else happens 25 | return cashGiven; 26 | } 27 | 28 | public double payGourmet(double cashGiven) { 29 | double priceGourmet=4.00; 30 | if (cashGiven >= priceGourmet) { 31 | this.cashInRegister+=priceGourmet; 32 | this.gourmetSold++; 33 | return cashGiven - priceGourmet; 34 | } 35 | // price of the gourmet lunch is 4.00 euros 36 | // if the given cash is at least the price of the lunch: 37 | // the price of lunch is added to register 38 | // the amount of sold lunch is incremented by one 39 | // method returns cashGiven - lunch price 40 | // if not enough money given, all is returned and nothing else happens 41 | return cashGiven; 42 | } 43 | 44 | public boolean payEconomical(LyyraCard card) { 45 | double priceEconomical=2.50; 46 | if (card.balance() >= priceEconomical) { 47 | this.economicalSold++; 48 | card.pay(priceEconomical); 49 | return true; 50 | } 51 | return false; 52 | // the price of the economical lunch is 2.50 euros 53 | // if the balance of the card is at least the price of the lunch: 54 | // the amount of sold lunches is incremented by one 55 | // the method returns true 56 | // if not, the method returns false 57 | } 58 | 59 | public boolean payGourmet(LyyraCard card) { 60 | double priceGourmet=4.00; 61 | if (card.balance() >= priceGourmet) { 62 | this.gourmetSold++; 63 | card.pay(priceGourmet); 64 | return true; 65 | } 66 | return false; 67 | // the price of the gourmet lunch is 4.00 euros 68 | // if the balance of the card is at least the price of the lunch: 69 | // the amount of sold lunches is incremented by one 70 | // the method returns true 71 | // if not, the method returns false 72 | } 73 | 74 | public void loadMoneyToCard(LyyraCard card, double sum) { 75 | if (sum > 0) { 76 | card.loadMoney(sum); 77 | this.cashInRegister+=sum; 78 | } 79 | } 80 | 81 | 82 | public String toString() { 83 | return "money in register " + cashInRegister + " economical lunches sold: " + economicalSold + " gourmet lunches sold: " + gourmetSold; 84 | } 85 | } -------------------------------------------------------------------------------- /week_4/083 HangmanFigure.java: -------------------------------------------------------------------------------- 1 | // do not touch this! 2 | 3 | import java.awt.Color; 4 | import java.awt.FontMetrics; 5 | import java.awt.Graphics; 6 | import javax.swing.JPanel; 7 | 8 | public class HangmanFigure extends JPanel { 9 | 10 | private HangmanLogic logic; 11 | private HangmanUserInterface userInterface; 12 | 13 | HangmanFigure(HangmanLogic logic, HangmanUserInterface ui) { 14 | super(); 15 | setBackground(Color.WHITE); 16 | this.logic = logic; 17 | this.userInterface = ui; 18 | 19 | } 20 | 21 | @Override 22 | public void paint(Graphics grphcs) { 23 | super.paint(grphcs); 24 | paintHangman(grphcs); 25 | 26 | this.userInterface.repaint(); 27 | } 28 | 29 | private void paintHangman(Graphics g) { 30 | int virheita = this.logic.numberOfFaults(); 31 | 32 | // piirretaan ukko 33 | int baseY = 200; 34 | 35 | if (virheita > 0) { // runko 36 | g.drawLine(90, baseY, 200, baseY); 37 | } 38 | if (virheita > 1) { // palkki 39 | g.drawLine(125, baseY, 125, baseY - 100); 40 | } 41 | if (virheita > 2) { 42 | g.drawLine(110, baseY, 125, baseY - 15); 43 | } 44 | if (virheita > 3) { 45 | g.drawLine(140, baseY, 125, baseY - 15); 46 | } 47 | if (virheita > 4) { // tuki 48 | g.drawLine(125, baseY - 100, 175, baseY - 100); 49 | } 50 | if (virheita > 5) { 51 | g.drawLine(125, baseY - 85, 140, baseY - 100); 52 | } 53 | if (virheita > 6) { // naru 54 | g.drawLine(175, baseY - 100, 175, baseY - 75); 55 | } 56 | if (virheita > 7) { // ruumis 57 | g.drawOval(170, baseY - 75, 10, 12); 58 | } 59 | if (virheita > 8) { 60 | g.drawOval(170, baseY - 65, 15, 25); 61 | } 62 | if (virheita > 9) { // kadet 63 | g.drawLine(160, baseY - 65, 170, baseY - 60); 64 | } 65 | if (virheita > 10) { 66 | g.drawLine(183, baseY - 60, 193, baseY - 65); 67 | } 68 | if (virheita > 11) { // jalat 69 | g.drawLine(165, baseY - 30, 170, baseY - 45); 70 | } 71 | if (virheita > 12) { 72 | g.drawLine(183, baseY - 45, 193, baseY - 30); 73 | } 74 | 75 | // näytä sana 76 | g.drawString("Sana: " + this.logic.hiddenWord(), 20, 250); 77 | 78 | // näytä virheiden lkm 79 | g.drawString("Virheitä: " + virheita, 20, 270); 80 | 81 | 82 | // näytä viesti 83 | g.drawString("Viesti: " + annaTilanne(), 20, 290); 84 | 85 | // näytä arvatut kirjaimet 86 | drawString(g, "Arvatut: " + this.logic.guessedLetters(), 20, 310, 240); 87 | } 88 | 89 | public String annaTilanne() { 90 | String salattuSana = this.logic.hiddenWord(); 91 | if (!salattuSana.contains("_")) { 92 | return "Voitit!"; 93 | } 94 | 95 | if (logic.numberOfFaults() > this.logic.losingFaultAmount()) { 96 | return "Hävisit!"; 97 | } 98 | 99 | return "Syötä kirjain!"; 100 | } 101 | 102 | // piirretään tekstiä joka vaihtaa riviä tarvittaessa 103 | public void drawString(Graphics g, String string, int x, int y, int width) { 104 | FontMetrics fontMetrics = g.getFontMetrics(); 105 | int lineHeight = fontMetrics.getHeight(); 106 | 107 | int curX = x; 108 | int curY = y; 109 | 110 | String[] words = string.split(" "); 111 | 112 | for (String word : words) { 113 | // Find out thw width of the word. 114 | int wordWidth = fontMetrics.stringWidth(word + " "); 115 | 116 | // If text exceeds the width, then move to next line. 117 | if (curX + wordWidth >= x + width) { 118 | curY += lineHeight; 119 | curX = x; 120 | } 121 | 122 | g.drawString(word, curX, curY); 123 | 124 | // Move over to the right for next word. 125 | curX += wordWidth; 126 | } 127 | } 128 | } 129 | --------------------------------------------------------------------------------