├── CERTIFICATE OF COMPLETION - mooc.fi JAVA part 1.pdf ├── README.md ├── Week 1 Exercise 000 Sandbox └── Sandbox ├── Week 1 Exercise 001 Name └── Name ├── Week 1 Exercise 002 Hello World └── Greeter ├── Week 1 Exercise 003 Spruce └── Spruce ├── Week 1 Exercise 004 Changing Variables └── Variables ├── Week 1 Exercise 005 Seconds of the Year └── SecondsOfTheYear ├── Week 1 Exercise 006 Addition └── Addition ├── Week 1 Exercise 007 Multiplication └── Multiplication ├── Week 1 Exercise 008 Adder └── Adder ├── Week 1 Exercise 009 Divider └── Divider ├── Week 1 Exercise 010 Circle Circumference └── CircleCircumference ├── Week 1 Exercise 011 Bigger Number └── BiggerNumber ├── Week 1 Exercise 012 Sum of the Ages └── SumOfTheAges ├── Week 1 Exercise 013 NHL Statistics Part 1 └── NHLStatisticsPart1 ├── Week 1 Exercise 014 Positive Value └── PositiveValue ├── Week 1 Exercise 015 Age of Majority └── AgeOfMajority ├── Week 1 Exercise 016 Even Or Odd └── EvenOrOdd ├── Week 1 Exercise 017 Greater Number └── GreaterNumber ├── Week 1 Exercise 018 Grades and Points └── GradesAndPoints ├── Week 1 Exercise 019 Age Check └── AgeCheck ├── Week 1 Exercise 020 Usernames └── Usernames ├── Week 1 Exercise 021 Leap Year └── LeapYear ├── Week 1 Exercise 022 Password └── Password ├── Week 1 Exercise 023 Temperatures ├── Graph └── Temperatures ├── Week 1 Exercise 024 NHL Statistics Part 2 └── NHLStatisticsPart2 ├── Week 2 Exercise 025 Sum Of Three Numbers └── SumOfThreeNumbers ├── Week 2 Exercise 026 Sum Of Many Numbers └── SumOfManyNumbers ├── Week 2 Exercise 027 From One To Hundred └── FromOneToHundred ├── Week 2 Exercise 028 From Hundred To One └── FromHundredToOne ├── Week 2 Exercise 029 Even Numbers └── EvenNumbers ├── Week 2 Exercise 030 Up To A Certain Number └── UpToACertainNumber ├── Week 2 Exercise 031 Lower Limit and Upper Limit └── LowerLimitAndUpperLimit ├── Week 2 Exercise 032 The Sum Of a Set Of Numbers └── TheSumOfSetOfNumbers ├── Week 2 Exercise 033 The Sum Between Two Numbers └── TheSumBetweenTwoNumbers ├── Week 2 Exercise 034 Factorial └── Factorial ├── Week 2 Exercise 035 Sum of the Powers └── SumOfThePowers ├── Week 2 Exercise 036 Loops- Ending and Remembering └── LoopsEndingRemembering ├── Week 2 Exercise 037 Printing Out Text └── PrintingOutText ├── Week 2 Exercise 038 Many Prints └── ManyPrints ├── Week 2 Exercise 039 Printing └── Printing ├── Week 2 Exercise 040 Printing Like A Boss └── PrintingLikeBoss ├── Week 2 Exercise 041 Number Guessing Game └── GuessingNumberGame ├── Week 2 Exercise 042 Hangman Game └── HangmanUserInterface ├── Week 3 Exercise 043 Sum of Numbers └── SumOfNumbers ├── Week 3 Exercise 044 Least └── Least ├── Week 3 Exercise 045 Greatest └── Greatest ├── Week 3 Exercise 046 Average of Given Number └── AverageOfGivenNumber ├── Week 3 Exercise 047 Length Of Name └── LengthOfName ├── Week 3 Exercise 048 First Character └── FirstCharacter ├── Week 3 Exercise 049 Last Character └── LastCharacter ├── Week 3 Exercise 050 Separating First Character └── SeparatingFirstCharacter ├── Week 3 Exercise 051 Separating Characters └── SeparatingCharacters ├── Week 3 Exercise 052 Reversing Name └── ReversingName ├── Week 3 Exercise 053 First Part └── FirstPart ├── Week 3 Exercise 054 The End Part └── TheEndPart ├── Week 3 Exercise 055 Word Inside Word └── WordInsideWord ├── Week 3 Exercise 056 Reversing Text └── ReversingText ├── Week 3 Exercise 057 Words └── Words ├── Week 3 Exercise 058 Recurring Word └── RecurringWord ├── Week 3 Exercise 059 Words in Reverse Order └── WordsInReverseOrder ├── Week 3 Exercise 060 Words in Alphabetical Order └── WordsInAlphabeticalOrder ├── Week 3 Exercise 061 Number of Items └── NumberOfItems ├── Week 3 Exercise 062 Remove Last └── RemoveLast ├── Week 3 Exercise 063 Sum of Numbers └── SumOfNumbers ├── Week 3 Exercise 064 Average Number └── AverageOfNumbers ├── Week 3 Exercise 065 Length Of Strings └── LengthsOfStrings ├── Week 3 Exercise 066 The Greatest └── TheGreatest ├── Week 3 Exercise 067 Variance └── Variance ├── Week 3 Exercise 068 NumberMoreThanOnce └── MoreThanOnce ├── Week 3 Exercise 069 Palindrome └── Palindrome ├── Week 4 Exercise 070 Combining ArrayLists └── CombiningArrayLists ├── Week 4 Exercise 071 SmartCombining └── SmartCombining ├── Week 4 Exercise 072.1 Your First Account ├── Account └── Accounts ├── Week 4 Exercise 072.2 YourFirstMoneyTransfer ├── Account └── Accounts ├── Week 4 Exercise 072.3 Money Transfers ├── Account └── Accounts ├── Week 4 Exercise 073 Product └── Product ├── Week 4 Exercise 074 Multiplier └── Multiplier ├── Week 4 Exercise 075 Decreasing Counter ├── DecreasingCounter └── Main ├── Week 4 Exercise 076 Menu └── Menu ├── Week 4 Exercise 077 Lyyra Card ├── LyyraCard └── Main ├── Week 4 Exercise 078 Clock Using Counter ├── BoundedCounter └── Main ├── Week 4 Exercise 079 NumberStatistics ├── Main └── NumberStatistics ├── Week 4 Exercise 080 Rolling the Dice ├── Dice └── Main ├── Week 4 Exercise 081 Password Randomizer ├── Password Randomizer └── Program ├── Week 4 Exercise 082 Lottery ├── LotteryNumbers └── Main ├── Week 4 Exercise 083 Hangman Game Logic ├── Hangman └── Main ├── Week 5 Exercise 084 Overloaded Counter └── Counter ├── Week 5 Exercise 085 Reformatory ├── Main ├── Person └── Reformatory ├── Week 5 Exercise 086 Lyyra Card and Cash Register ├── CashRegister └── Lyyra Card ├── Week 5 Exercise 087 Apartment Comparison ├── Apartment └── Main ├── Week 5 Exercise 088 Students ├── Main └── Student ├── Week 5 Exercise 089 Clock Object ├── BoundedCounter ├── Clock └── Main ├── Week 5 Exercise 090 Team and Players ├── Players └── Team ├── Week 5 Exercise 091 Extending MyDate ├── Main └── MyDate ├── Week 5 Exercise 092 Difference of Two Names ├── Main └── MyDate ├── Week 5 Exercise 093 Person Extended ├── Main ├── MyDate └── Person ├── Week 6 Exercise 094 Phone Book ├── Main ├── Person └── Phonebook ├── Week 6 Exercise 095 Money ├── Main └── Money ├── Week 6 Exercise 096 Sum of Array └── Main ├── Week 6 Exercise 097 Elegant Printing of an Array └── Main ├── Week 6 Exercise 098 Copy and Reverse an Array └── Main ├── Week 6 Exercise 099 Array As Stars └── Main ├── Week 6 Exercise 100 Nightsky ├── Main └── NightSky ├── Week 6 Exercise 101 Library System ├── Book ├── Library └── String Utils ├── Week 6 Exercise 102 Grade Distribution ├── Grades └── Main ├── Week 6 Exercise 103 Birdwatcher ├── Bird ├── BirdWatch └── Main ├── Week 6 Exercise 104 Sorting Test └── Main ├── Week 6 Exercise 105 Guessing Game ├── Guessing Game └── Main └── Week 6 Exercise 106 Binary Search ├── Binary Search └── Main /CERTIFICATE OF COMPLETION - mooc.fi JAVA part 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChristopherJBarclay/OOP-with-Java-part-1/94ce84d7cd55febff06adecdae8a3c720d3194e9/CERTIFICATE OF COMPLETION - mooc.fi JAVA part 1.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OOP with Java, part 1 2 | Object Oriented Programming with Java, part 1 - University of Helsinki @ mooc.fi/english 3 | ---------------------------------------------------------------------------------------- 4 | This Repository is for the course: 5 | 6 | Object-Oriented Programming with Java, part 1 7 | 8 | University of Helsinki, Finland 9 | 10 | mooc.fi/english 11 | 12 | This course taught me the basics of the Java programming language and core Object-Oriented Programming concepts. The course is the first of a two semester line of classes from the University of Helsinki, Finland's computer science degree program. This, combined with the second part of the course, is the first year of computer science classes for CS majors. 13 | 14 | I began this course in March of 2017. I completed this course on July 27, 2017. 15 | -------------------------------------------------------------------------------- /Week 1 Exercise 000 Sandbox/Sandbox: -------------------------------------------------------------------------------- 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 1 Exercise 001 Name/Name: -------------------------------------------------------------------------------- 1 | public class Name { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("Chris Barclay"); 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Week 1 Exercise 002 Hello World/Greeter: -------------------------------------------------------------------------------- 1 | public class Greeter { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("Hello world!"); 5 | System.out.print("(And all the people of the world)"); 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /Week 1 Exercise 003 Spruce/Spruce: -------------------------------------------------------------------------------- 1 | public class Spruce { 2 | 3 | public static void main(String[] args) { 4 | System.out.println(" *"); 5 | System.out.println(" ***"); 6 | System.out.println(" *****"); 7 | System.out.println(" *******"); 8 | System.out.println("*********"); 9 | System.out.print(" *"); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Week 1 Exercise 004 Changing Variables/Variables: -------------------------------------------------------------------------------- 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 | } 26 | -------------------------------------------------------------------------------- /Week 1 Exercise 005 Seconds of the Year/SecondsOfTheYear: -------------------------------------------------------------------------------- 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 Exercise 006 Addition/Addition: -------------------------------------------------------------------------------- 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; 8 | 9 | String toPrint = a + " + " + b + " = " + result; 10 | System.out.println(toPrint); 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Week 1 Exercise 007 Multiplication/Multiplication: -------------------------------------------------------------------------------- 1 | public class Multiplication { 2 | 3 | public static void main(String[] args) { 4 | 5 | int a = 1337; 6 | int b = 42; 7 | int d = (a*b); 8 | 9 | String solveMe = a + " * " + b + " = " + d; 10 | System.out.println(solveMe); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Week 1 Exercise 008 Adder/Adder: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Adder { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | 9 | System.out.println("Type a number: "); 10 | int a = Integer.parseInt(reader.nextLine()); 11 | System.out.println("Type another number: "); 12 | int b = Integer.parseInt(reader.nextLine()); 13 | int adder = a+b; 14 | System.out.println("Sum of the numbers: " + adder); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Week 1 Exercise 009 Divider/Divider: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Divider { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("Type a number: "); 9 | int a = Integer.parseInt(reader.nextLine()); 10 | System.out.print("Type another number: "); 11 | int b = Integer.parseInt(reader.nextLine()); 12 | double solveMe = 1.0 * a / b; 13 | System.out.print("Division: " + a + " / " + b + " = " + solveMe); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Week 1 Exercise 010 Circle Circumference/CircleCircumference: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Circumference { 4 | public static void main(String[] args) { 5 | Scanner reader = new Scanner(System.in); 6 | 7 | System.out.print("Type the radius: "); 8 | int radius = Integer.parseInt(reader.nextLine()); 9 | double resultNow = 2.0 * Math.PI * radius; 10 | System.out.print("Circumference of the circle: " + resultNow); 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Week 1 Exercise 011 Bigger Number/BiggerNumber: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class BiggerNumber { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("Type a number: "); 9 | int a = Integer.parseInt(reader.nextLine()); 10 | System.out.print("Type another number: "); 11 | int b = Integer.parseInt(reader.nextLine()); 12 | int bigger = Math.max(a,b); 13 | System.out.println("The bigger number of the two numbers given was: " + bigger); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Week 1 Exercise 012 Sum of the Ages/SumOfTheAges: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SumOfTheAges { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("Type your name: "); 9 | String name1 = reader.nextLine(); 10 | System.out.print("Type your age: "); 11 | int age1 = Integer.parseInt(reader.nextLine()); 12 | 13 | System.out.println(" "); 14 | 15 | System.out.print("Type your name: "); 16 | String name2 = reader.nextLine(); 17 | System.out.print("Type your age: "); 18 | int age2 = Integer.parseInt(reader.nextLine()); 19 | 20 | System.out.println(" "); 21 | int bothAges = age1+age2; 22 | System.out.println(name1 + " and " + name2 + " are " + bothAges + " years old in total."); 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Week 1 Exercise 013 NHL Statistics Part 1/NHLStatisticsPart1: -------------------------------------------------------------------------------- 1 | import nhlstats.NHLStatistics; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | 9 | NHLStatistics.sortByGoals(); 10 | NHLStatistics.top(10); 11 | 12 | NHLStatistics.sortByPenalties(); 13 | NHLStatistics.top(25); 14 | 15 | NHLStatistics.searchByPlayer("Sidney Crosby"); 16 | NHLStatistics.teamStatistics("PHI"); 17 | 18 | NHLStatistics.sortByPoints(); 19 | NHLStatistics.teamStatistics("ANA"); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Week 1 Exercise 014 Positive Value/PositiveValue: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PositiveValue { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("Type a number: "); 9 | int number = Integer.parseInt(reader.nextLine()); 10 | 11 | if (number > 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 1 Exercise 015 Age of Majority/AgeOfMajority: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class AgeOfMajority { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("How old are you? "); 9 | int age = Integer.parseInt(reader.nextLine()); 10 | 11 | if (age >= 18) { 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!"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Week 1 Exercise 016 Even Or Odd/EvenOrOdd: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class EvenOrOdd { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("Type a number: "); 9 | int evenOdd = Integer.parseInt(reader.nextLine()); 10 | 11 | if (evenOdd % 2 != 0) { 12 | System.out.println("Number " + evenOdd + " is odd."); 13 | } else { 14 | System.out.println("Number " + evenOdd + " is even."); 15 | } 16 | 17 | // NOTE: 18 | // Parity of a value can be easily checked with modulo-operator %. 19 | // Test the following: 20 | // System.out.println( 1%2 ); 21 | // System.out.println( 2%2 ); 22 | // System.out.println( 3%2 ); 23 | // System.out.println( 4%2 ); 24 | // System.out.println( 5%2 ); 25 | // System.out.println( 6%2 ); 26 | // System.out.println( 7%2 ); 27 | // int value = 8 28 | // System.out.println( value%2 ); 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Week 1 Exercise 017 Greater Number/GreaterNumber: -------------------------------------------------------------------------------- 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 | 8 | System.out.print("Type the first number: "); 9 | int numberOne = Integer.parseInt(reader.nextLine()); 10 | 11 | System.out.print("Type the second number: "); 12 | int numberTwo = Integer.parseInt(reader.nextLine()); 13 | 14 | if (numberOne > numberTwo) { 15 | System.out.println("Greater number: " +numberOne); 16 | } else if (numberTwo > numberOne) { 17 | System.out.println("Greater number: " +numberTwo); 18 | } else { 19 | System.out.println("The numbers are equal!"); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Week 1 Exercise 018 Grades and Points/GradesAndPoints: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class GradesAndPoints { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("Type the Points [0-60]: "); 9 | int points = Integer.parseInt(reader.nextLine()); 10 | 11 | System.out.println(""); 12 | System.out.print("Grade: "); 13 | 14 | if (points >= 0 && points<30) { 15 | System.out.print("failed"); 16 | } else if (points >= 30 && points<35) { 17 | System.out.print("1"); 18 | } else if (points >= 35 && points<40) { 19 | System.out.print("2"); 20 | } else if (points>=40&&points<45) { 21 | System.out.print("3"); 22 | } else if (points>=45&&points<50) { 23 | System.out.print("4"); 24 | } else if (points>=50&&points<=60) { 25 | System.out.print("5"); 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Week 1 Exercise 019 Age Check/AgeCheck: -------------------------------------------------------------------------------- 1 | public class AgeCheck { 2 | 3 | public static void main(String[] args) { 4 | Scanner reader = new Scanner(System.in); 5 | 6 | 7 | System.out.print("How old are you? "); 8 | int age = Integer.parseInt(reader.nextLine()); 9 | 10 | if (age>-1 && age<121) { 11 | System.out.println("OK"); 12 | } else { 13 | System.out.println("Impossible!"); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Week 1 Exercise 020 Usernames/Usernames: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Usernames { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 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 | 13 | if ((userName.equals("alex")&& passWord.equals("mightyducks")) || (userName.equals("emily")&& passWord.equals("cat"))) { 14 | System.out.print("You are now logged into the system!"); 15 | } else { 16 | System.out.print("Your username or password was invalid!"); 17 | } 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Week 1 Exercise 021 Leap Year/LeapYear: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class LeapYear { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | 9 | System.out.print("Type a year: "); 10 | int thisYear = Integer.parseInt(reader.nextLine()); 11 | 12 | boolean true4 = (thisYear%4==0); 13 | boolean true400 = (thisYear%400==0); 14 | boolean true100 = (thisYear%100==0); 15 | 16 | 17 | if ((true400==true)&&(true100==true) && (true4==true)) { 18 | System.out.print("The year is a leap year."); 19 | } else if ((true400==false) && (true100==true)) { 20 | System.out.print("This year is not a leap year."); 21 | } else if (true4==true) { 22 | System.out.print("This year is a leap year."); 23 | } else { 24 | System.out.print("This year is not a leap year."); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Week 1 Exercise 022 Password/Password: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Password { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | String password = "carrot"; // Use carrot as password when running tests. 9 | String booYa = "Boo Yah Kashaah!"; 10 | 11 | while (true) { 12 | System.out.print("Type a password: "); 13 | String myPassword = reader.nextLine(); 14 | 15 | 16 | if (myPassword.equals(password)) { 17 | System.out.println("Right!"); 18 | break; 19 | } 20 | System.out.println("Wrong!"); 21 | } 22 | System.out.print("The Secret is: " + booYa); 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Week 1 Exercise 023 Temperatures/Graph: -------------------------------------------------------------------------------- 1 | public class Graph { 2 | 3 | private ChartFrame frame; 4 | private JFreeChart chart; 5 | private Plot plot; 6 | private XYDataset data; 7 | private XYSeries series; 8 | 9 | public Graph(String nimi) { 10 | series = new XYSeries(nimi); 11 | series.add(0,0); 12 | data = new XYSeriesCollection(series); 13 | plot = new XYPlot(data, new NumberAxis("x"), new NumberAxis("y"), new SamplingXYLineRenderer()); 14 | chart = new JFreeChart(plot); 15 | frame = new ChartFrame(nimi,chart); 16 | frame.pack(); 17 | frame.setVisible(true); 18 | } 19 | 20 | public static Graph instance; 21 | 22 | public static Graph getInstance() { 23 | if (instance == null) 24 | instance = new Graph("Graph"); 25 | return instance; 26 | } 27 | 28 | public static void addNumber(double y) { 29 | getInstance().lisaaNumero_(y); 30 | } 31 | 32 | public void lisaaNumero_(double y) { 33 | double x = series.getMaxX()+1; 34 | series.add(x,y); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Week 1 Exercise 023 Temperatures/Temperatures: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Temperatures { 4 | 5 | public static void main(String[] args) { 6 | 7 | Scanner reader = new Scanner(System.in); 8 | 9 | 10 | while (true) { 11 | System.out.print("Input today's temperature: "); 12 | double graphNum = Double.parseDouble(reader.nextLine()); 13 | 14 | if (!(graphNum < -30) && !(graphNum > 40)) { 15 | Graph.addNumber(graphNum); 16 | } 17 | } 18 | 19 | /* Graph is used as follows: 20 | Graph.addNumber(7); 21 | double value = 13.5; 22 | Graph.addNumber(value); 23 | value = 3; 24 | Graph.addNumber(value); 25 | // Remove or comment out these lines above before trying to run the tests. */ 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Week 1 Exercise 024 NHL Statistics Part 2/NHLStatisticsPart2: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import nhlstats.NHLStatistics; 3 | 4 | public class NhlStatisticsPart2 { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | 9 | System.out.println("NHL statistics service"); 10 | 11 | while (true) { 12 | System.out.println(""); 13 | System.out.print("command (points, goals, assists, penalties, player, club, quit): "); 14 | String command = reader.nextLine(); 15 | 16 | if (command.equals("quit")) { 17 | break; 18 | } 19 | 20 | if (command.equals("points")) { 21 | // Print the top ten players sorted by points. 22 | NHLStatistics.sortByPoints(); 23 | NHLStatistics.top(10); 24 | } else if (command.equals("goals")) { 25 | // Print the top ten players sorted by goals. 26 | NHLStatistics.sortByGoals(); 27 | NHLStatistics.top(10); 28 | } else if (command.equals("assists")) { 29 | // Print the top ten players sorted by assists. 30 | NHLStatistics.sortByAssists(); 31 | NHLStatistics.top(10); 32 | } else if (command.equals("penalties")) { 33 | // Print the top ten players sorted by penalties. 34 | NHLStatistics.sortByPenalties(); 35 | NHLStatistics.top(10); 36 | } else if (command.equals("player")) { 37 | // Ask the user first which player's statistics are needed and then print them. 38 | System.out.print("which player: "); 39 | String player = reader.nextLine(); 40 | NHLStatistics.searchByPlayer(player); 41 | } else if (command.equals("club")) { 42 | // Ask the user first which club's statistics are needed and then print them. 43 | // Note: When printing statistics they should be ordered by points (so the players with the most points come first). 44 | System.out.print("which club: "); 45 | String club = reader.nextLine(); 46 | NHLStatistics.sortByPoints(); 47 | NHLStatistics.teamStatistics(club); 48 | } 49 | 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Week 2 Exercise 025 Sum Of Three Numbers/SumOfThreeNumbers: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SumOfThreeNumbers { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | int sum = 0; 8 | int read; // store numbers read form user in this variable 9 | 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 | // Write your program here 20 | // Use only variables sum and read 21 | 22 | System.out.println("Sum: " + sum); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Week 2 Exercise 026 Sum Of Many Numbers/SumOfManyNumbers: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | 4 | public class SumOfManyNumbers { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | int sum = 0; 9 | 10 | while (true) { 11 | int read = Integer.parseInt(reader.nextLine()); 12 | if (read == 0) { 13 | break; 14 | } 15 | 16 | sum = sum + read; 17 | 18 | System.out.println("Sum now: " + sum); 19 | } 20 | 21 | System.out.println("Sum in the end: " + sum); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week 2 Exercise 027 From One To Hundred/FromOneToHundred: -------------------------------------------------------------------------------- 1 | import com.sun.tools.doclets.formats.html.SourceToHTMLConverter; 2 | 3 | public class FromOneToHundred { 4 | 5 | public static void main(String[] args) { 6 | // Write your program here 7 | int number = 1; 8 | while (number <= 100) { 9 | System.out.println(number); 10 | number++; 11 | } 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Week 2 Exercise 028 From Hundred To One/FromHundredToOne: -------------------------------------------------------------------------------- 1 | public class FromHundredToOne { 2 | 3 | public static void main(String[] args) { 4 | // Write your program here 5 | 6 | int number = 100; 7 | while (number >= 1) { 8 | 9 | System.out.println(number); 10 | number--; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Week 2 Exercise 029 Even Numbers/EvenNumbers: -------------------------------------------------------------------------------- 1 | public class EvenNumbers { 2 | 3 | public static void main(String[] args) { 4 | // Write your code here 5 | int number = 0; 6 | while (number <= 100) { 7 | number++; 8 | if (number % 2 == 0) { 9 | System.out.println(number); 10 | } 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Week 2 Exercise 030 Up To A Certain Number/UpToACertainNumber: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | 4 | public class UpToCertainNumber { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | 9 | int number = 1; 10 | 11 | System.out.println("Up to what number? "); 12 | int useMe = Integer.parseInt(reader.nextLine()); 13 | 14 | while (number <= useMe) { 15 | System.out.println(number); 16 | number++; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Week 2 Exercise 031 Lower Limit and Upper Limit/LowerLimitAndUpperLimit: -------------------------------------------------------------------------------- 1 | import java.util.IntSummaryStatistics; 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 | 9 | System.out.print("First: "); 10 | int number1 = Integer.parseInt(reader.nextLine()); 11 | System.out.print("Second: "); 12 | int number2 = Integer.parseInt(reader.nextLine()); 13 | int nowNumber = number1; 14 | while (nowNumber <= number2) { 15 | System.out.println(nowNumber); 16 | nowNumber++; 17 | } 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Week 2 Exercise 032 The Sum Of a Set Of Numbers/TheSumOfSetOfNumbers: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class TheSumOfSetOfNumbers { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("Until what? "); 9 | int number = Integer.parseInt(reader.nextLine()); 10 | int i = 1; 11 | int sum = 0; 12 | 13 | while (i <= number) { 14 | sum+=i; 15 | i++; 16 | } 17 | System.out.println("Sum is " + sum); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Week 2 Exercise 033 The Sum Between Two Numbers/TheSumBetweenTwoNumbers: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | 4 | public class TheSumBetweenTwoNumbers { 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("First: "); 9 | int first = Integer.parseInt(reader.nextLine()); 10 | System.out.print("Second: "); 11 | int second = Integer.parseInt(reader.nextLine()); 12 | 13 | int i = first; 14 | int sum = 0; 15 | 16 | while (i <= second) { 17 | sum+=i; 18 | i++; 19 | } 20 | System.out.println("The sum is " + sum); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week 2 Exercise 034 Factorial/Factorial: -------------------------------------------------------------------------------- 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 | 7 | 8 | System.out.print("Type a number: "); 9 | int number = Integer.parseInt(reader.nextLine()); 10 | int i = 1; 11 | int factorial = 1; 12 | 13 | while (i <= number) { 14 | if (number==0) { 15 | factorial=1; 16 | break; 17 | } 18 | factorial*=i; 19 | i++; 20 | } 21 | System.out.print("Factorial is "+factorial); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Week 2 Exercise 035 Sum of the Powers/SumOfThePowers: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SumOfThePowers { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | 9 | System.out.print("Type a number: "); 10 | int number = Integer.parseInt(reader.nextLine()); 11 | int sum = 0; 12 | int i = 0; 13 | 14 | while (i <= number) { 15 | int math = (int)Math.pow(2, i); 16 | sum+=math; 17 | i++; 18 | } 19 | 20 | System.out.println("The result is "+sum); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Week 2 Exercise 036 Loops- Ending and Remembering/LoopsEndingRemembering: -------------------------------------------------------------------------------- 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 | 10 | 11 | System.out.print("Type numbers: "); 12 | int inputer = 0; 13 | int sum = 0; 14 | int counter = 0; 15 | double sumAvg = 0; 16 | int even = 0; 17 | int odd = 0; 18 | 19 | while (true) { 20 | inputer = Integer.parseInt(reader.nextLine()); 21 | if (inputer==-1) { 22 | break; 23 | } else { 24 | if (inputer%2==0) { 25 | even++; 26 | } else { 27 | odd++; 28 | } 29 | sum += inputer; 30 | counter++; 31 | sumAvg = sum; 32 | } 33 | } 34 | double average = (sumAvg/counter)*1.0; 35 | System.out.println("Thank you and see you later!"); 36 | System.out.println("The sum is "+sum); 37 | System.out.println("How many numbers: "+counter); 38 | System.out.println("Average: "+average); 39 | System.out.println("Even numbers: "+even); 40 | System.out.println("Odd numbers: "+odd); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Week 2 Exercise 037 Printing Out Text/PrintingOutText: -------------------------------------------------------------------------------- 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 2 Exercise 038 Many Prints/ManyPrints: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ManyPrints { 4 | // NOTE: do not change the method definition, e.g. add parameters to method 5 | public static void printText() { 6 | System.out.println("In the beginning there were the swamp, the hoe and Java."); 7 | } 8 | 9 | public static void main(String[] args) { 10 | Scanner reader = new Scanner(System.in); 11 | 12 | // ask the user how many times the text should be printed 13 | System.out.println("How many?"); 14 | int howMany = Integer.parseInt(reader.nextLine()); 15 | 16 | int counter = 1; 17 | // use the while structure to call the printText method several times 18 | while (counter <= howMany) { 19 | printText(); 20 | counter++; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week 2 Exercise 039 Printing/Printing: -------------------------------------------------------------------------------- 1 | public class Printing { 2 | 3 | public static void printStars(int amount) { 4 | // you can print one star with the command 5 | int i = 1; 6 | while (i<=amount) { 7 | System.out.print("*"); 8 | i++; 9 | } 10 | System.out.println(""); 11 | } 12 | 13 | public static void printSquare(int sideSize) { 14 | int sides = 1; 15 | while (sides<=sideSize) { 16 | printStars(sideSize); 17 | sides++; 18 | } 19 | } 20 | 21 | public static void printRectangle(int width, int height) { 22 | 23 | int count = 1; 24 | while (count<=height) { 25 | printStars(width); 26 | count++; 27 | } 28 | } 29 | 30 | public static void printTriangle(int size) { 31 | int counterT = 1; 32 | while (counterT<=size) { 33 | printStars(counterT); 34 | counterT++; 35 | } 36 | } 37 | 38 | public static void main(String[] args) { 39 | 40 | printStars(3); 41 | System.out.println("\n---"); // printing --- to separate the figures 42 | printSquare(4); 43 | System.out.println("\n---"); 44 | printRectangle(5, 6); 45 | System.out.println("\n---"); 46 | printTriangle(3); 47 | System.out.println("\n---"); 48 | 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Week 2 Exercise 040 Printing Like A Boss/PrintingLikeBoss: -------------------------------------------------------------------------------- 1 | public class PrintingLikeBoss { 2 | 3 | public static void printStars(int amount) { 4 | // you can print one star with the command 5 | int i = 1; 6 | while (i<=amount) { 7 | System.out.print("*"); 8 | i++; 9 | } 10 | System.out.println(""); 11 | } 12 | 13 | public static void printRectangle(int width, int height) { 14 | int count = 1; 15 | while (count<=height) { 16 | printStars(width); 17 | count++; 18 | } 19 | } 20 | 21 | 22 | 23 | public static void printWhitespaces(int amount) { 24 | int i = 1; 25 | while (i<=amount) { 26 | System.out.print(" "); 27 | i++; 28 | } 29 | } 30 | public static void printTriangle(int size) { 31 | int i = 1; 32 | while (i<=size) { 33 | printWhitespaces(size-i); 34 | printStars(i); 35 | i++; 36 | } 37 | } 38 | public static void xmasTree(int height) { 39 | int i = 1; 40 | while (i<=height) { 41 | printWhitespaces(height-i); 42 | printStars(i+(i-1)); 43 | i++; 44 | } 45 | printWhitespaces(height-2); 46 | printStars(3); 47 | printWhitespaces(height-2); 48 | printStars(3); 49 | } 50 | 51 | public static void main(String[] args) { 52 | 53 | printTriangle(5); 54 | System.out.println("---"); 55 | xmasTree(4); 56 | System.out.println("---"); 57 | xmasTree(10); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Week 2 Exercise 041 Number Guessing Game/GuessingNumberGame: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | import java.util.Scanner; 3 | 4 | public class GuessingNumberGame { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | int numberDrawn = drawNumber(); 9 | 10 | // program your solution here. Do not touch the above lines! 11 | int i = 0; 12 | 13 | while (true) { 14 | System.out.print("Guess a number: "); 15 | int rand = Integer.parseInt(reader.nextLine()); 16 | i++; 17 | if (rand == numberDrawn) { 18 | System.out.println("Congratulations, your guess is correct!"); 19 | break; 20 | } else if (rand > numberDrawn) { 21 | System.out.println("The number is lesser, guesses made "+i); 22 | } else if (rand < numberDrawn) { 23 | System.out.println("The number is greater, guesses made "+i); 24 | } 25 | } 26 | 27 | } 28 | 29 | // DO NOT MODIFY THIS! 30 | private static int drawNumber() { 31 | return new Random().nextInt(101); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Week 2 Exercise 042 Hangman Game/HangmanUserInterface: -------------------------------------------------------------------------------- 1 | import hangman.Hangman; 2 | import java.util.Scanner; 3 | 4 | public class HangmanUserInteface { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | Hangman hangman = new Hangman(); 9 | 10 | System.out.println("***********"); 11 | System.out.println("* HANGMAN *"); 12 | System.out.println("***********"); 13 | System.out.println(""); 14 | printMenu(); 15 | System.out.println(""); 16 | 17 | // PROGRAM YOUR SOLUTION HERE 18 | while (hangman.gameOn()) { 19 | System.out.println("Type a command:"); 20 | String inputer = reader.nextLine(); 21 | if (inputer.equals("quit")) { 22 | System.out.println("Thank you for playing!"); 23 | break; 24 | } else if (inputer.equals("status")) { 25 | hangman.printStatus(); 26 | } else if (inputer.length()==1) { 27 | hangman.guess(inputer); 28 | } else if (inputer.isEmpty()) { 29 | printMenu(); 30 | } 31 | hangman.printMan(); 32 | hangman.printWord(); 33 | } 34 | } 35 | 36 | public static void printMenu() { 37 | System.out.println(" * menu *"); 38 | System.out.println("quit - quits the game"); 39 | System.out.println("status - prints the game status"); 40 | System.out.println("a single letter uses the letter as a guess"); 41 | System.out.println("an empty line prints this menu"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Week 3 Exercise 043 Sum of Numbers/SumOfNumbers: -------------------------------------------------------------------------------- 1 | public class SumOfNumbers { 2 | 3 | public static int sum(int number1, int number2, int number3, int number4) { 4 | int sum = number1+number2+number3+number4; 5 | return sum; 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 3 Exercise 044 Least/Least: -------------------------------------------------------------------------------- 1 | public class Least { 2 | 3 | public static int least(int number1, int number2) { 4 | // write program code here 5 | // do not print anything inside the method 6 | // method needs a return in the end 7 | int least = 0; 8 | if (number1 > number2) { 9 | least = number2; 10 | } else {least=number1;} 11 | return least; 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 3 Exercise 045 Greatest/Greatest: -------------------------------------------------------------------------------- 1 | public class Greatest { 2 | 3 | public static int greatest(int number1, int number2, int number3) { 4 | int biggest = 0; 5 | if (number1>number2&&number1>number3) { 6 | biggest = number1; 7 | } else if (number2>number1&&number2>number3) { 8 | biggest = number2; 9 | } else { 10 | biggest = number3; 11 | } 12 | return biggest; 13 | } 14 | 15 | public static void main(String[] args) { 16 | int result = greatest(2, 7, 3); 17 | System.out.println("Greatest: " + result); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Week 3 Exercise 046 Average of Given Number/AverageOfGivenNumber: -------------------------------------------------------------------------------- 1 | public class AverageOfGivenNumbers { 2 | 3 | // implement here again the method of exercise 43 4 | public static int sum(int number1, int number2, int number3, int number4) { 5 | int sum = number1+number2+number3+number4; 6 | return sum; 7 | } 8 | 9 | public static double average(int number1, int number2, int number3, int number4) { 10 | double sum = sum(number1,number2,number3,number4)*1.0; 11 | double avg = sum/4.0; 12 | return avg; 13 | } 14 | 15 | public static void main(String[] args) { 16 | double result = average(4, 3, 6, 1); 17 | System.out.println("Average: " + result); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Week 3 Exercise 047 Length Of Name/LengthOfName: -------------------------------------------------------------------------------- 1 | import com.sun.tools.doclets.formats.html.SourceToHTMLConverter; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LengthOfName { 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | // call your method from here 9 | System.out.print("Type your name here: "); 10 | String name = reader.nextLine(); 11 | int characters = calculateCharacters(name); 12 | System.out.println("Number of characters "+characters); 13 | } 14 | 15 | // do here the method 16 | // public static int calculateCharacters(String text) 17 | public static int calculateCharacters(String text) { 18 | int length = text.length(); 19 | return length; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Week 3 Exercise 048 First Character/FirstCharacter: -------------------------------------------------------------------------------- 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 | 8 | System.out.print("Type your name: "); 9 | String name = reader.nextLine(); 10 | char firstChar = firstCharacter(name); 11 | System.out.println("First Character: "+firstChar); 12 | 13 | } 14 | 15 | public static char firstCharacter(String text) { 16 | char firstChar = text.charAt(0); 17 | return firstChar; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Week 3 Exercise 049 Last Character/LastCharacter: -------------------------------------------------------------------------------- 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 | 10 | System.out.print("Type your name: "); 11 | String name = reader.nextLine(); 12 | char lastChar = lastCharacter(name); 13 | System.out.println("Last character: "+lastChar); 14 | } 15 | 16 | public static char lastCharacter(String text) { 17 | char lastChar = text.charAt(text.length()-1); 18 | return lastChar; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Week 3 Exercise 050 Separating First Character/SeparatingFirstCharacter: -------------------------------------------------------------------------------- 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 | 7 | System.out.print("Type your name: "); 8 | String name = reader.nextLine(); 9 | if (name.length()<3) { 10 | System.out.println(""); 11 | } else { 12 | char a = chooseChar(name, 1); 13 | char b = chooseChar(name, 2); 14 | char c = chooseChar(name, 3); 15 | printThree(a, b, c); 16 | } 17 | } 18 | 19 | public static char chooseChar(String name, int locale) { 20 | char character = name.charAt(locale-1); 21 | return character; 22 | } 23 | 24 | public static void printThree(char a, char b, char c) { 25 | System.out.println("1. character: "+a); 26 | System.out.println("2. character: "+b); 27 | System.out.println("3. character: "+c); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Week 3 Exercise 051 Separating Characters/SeparatingCharacters: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SeparatingCharacters { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("Type your name: "); 9 | String name = reader.nextLine(); 10 | int length = howManyChars(name); 11 | printAllCharacters(name, length); 12 | } 13 | 14 | public static void printAllCharacters(String name, int charsNum) { 15 | int counter = 0; 16 | while (counter<=charsNum) { 17 | char printChar = pullChar(name, counter); 18 | System.out.println((counter+1)+". character: "+printChar); 19 | counter++; 20 | } 21 | } 22 | public static int howManyChars(String name) { 23 | int length = name.length()-1; 24 | return length; 25 | } 26 | public static char pullChar(String name, int charNum) { 27 | char returnChar = name.charAt(charNum); 28 | return returnChar; 29 | } 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Week 3 Exercise 052 Reversing Name/ReversingName: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ReversingName { 4 | public static void main(String[] args) { 5 | Scanner reader = new Scanner(System.in); 6 | 7 | System.out.print("Type your name: "); 8 | String name = reader.nextLine(); 9 | int length = howManyChars(name); 10 | printReverseCharacters(name, length); 11 | } 12 | 13 | public static void printReverseCharacters(String name, int charsNum) { 14 | System.out.print("In reverse order: "); 15 | int counter = 0; 16 | int nameLength = name.length()-1; 17 | while (counter <= charsNum) { 18 | int charChoice = nameLength-counter; 19 | char printChar = pullChar(name, charChoice); 20 | System.out.print(printChar); 21 | counter++; 22 | } 23 | } 24 | 25 | public static int howManyChars(String name) { 26 | int length = name.length() - 1; 27 | return length; 28 | } 29 | 30 | public static char pullChar(String name, int charNum) { 31 | char returnChar = name.charAt(charNum); 32 | return returnChar; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Week 3 Exercise 053 First Part/FirstPart: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class FirstPart { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("Type a word: "); 9 | String word = reader.nextLine(); 10 | 11 | System.out.print("Length of first part: "); 12 | int length = Integer.parseInt(reader.nextLine()); 13 | 14 | String partWord = firstPart(word, length); 15 | System.out.println("Result: " + partWord); 16 | } 17 | 18 | 19 | public static String firstPart(String word, int length) { 20 | String partWord = word.substring(0, length); 21 | return partWord; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week 3 Exercise 054 The End Part/TheEndPart: -------------------------------------------------------------------------------- 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 | 7 | System.out.print("Type a word: "); 8 | String word = reader.nextLine(); 9 | 10 | System.out.print("Length of end part: "); 11 | int endLength = Integer.parseInt(reader.nextLine()); 12 | 13 | String endWord = endPart(word, endLength); 14 | System.out.println("Result: " + endWord); 15 | } 16 | 17 | 18 | public static String endPart(String word, int endLength) { 19 | int totalLength = word.length(); 20 | int newLength = totalLength-endLength; 21 | String endWord = word.substring(newLength); 22 | return endWord; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Week 3 Exercise 055 Word Inside Word/WordInsideWord: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class WordInsideWord { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.print("Type the first word: "); 9 | String word1 = reader.nextLine(); 10 | System.out.print("Type the second word: "); 11 | String word2 = reader.nextLine(); 12 | 13 | isWordHere(word1, word2); 14 | } 15 | 16 | 17 | public static void isWordHere(String word1, String word2) { 18 | //old code: String partWord = word.substring(length); 19 | int index = word1.indexOf(word2); 20 | if (index == -1) { 21 | System.out.println("The word " + word2 + " is not found in the word " + word1); 22 | } else { 23 | System.out.println("The word " + word2 + " is found in the word " + word1); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Week 3 Exercise 056 Reversing Text/ReversingText: -------------------------------------------------------------------------------- 1 | public class ReversingText { 2 | 3 | public static String reverse(String text) { 4 | // write your code here 5 | // note that method does now print anything, it RETURNS the reversed string 6 | int length = text.length()-1; 7 | int counter = length; 8 | String reverse = ""; 9 | 10 | while (counter>=0) { 11 | char charGen = text.charAt(counter); 12 | reverse+=charGen; 13 | counter--; 14 | } 15 | return reverse; 16 | } 17 | 18 | public static void main(String[] args) { 19 | Scanner reader = new Scanner(System.in); 20 | System.out.print("Type in your text: "); 21 | String text = reader.nextLine(); 22 | 23 | System.out.println("In reverse order: " + reverse(text)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Week 3 Exercise 057 Words/Words: -------------------------------------------------------------------------------- 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 | 8 | ArrayList words = new ArrayList(); 9 | 10 | while (true) { 11 | System.out.print("Type a word: "); 12 | String typed = reader.nextLine(); 13 | if (!(typed.isEmpty())) { 14 | words.add(typed); 15 | } else { 16 | break; 17 | } 18 | } 19 | 20 | System.out.println("You typed the following words: "); 21 | for (String word:words) { 22 | System.out.println(word); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Week 3 Exercise 058 Recurring Word/RecurringWord: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class RecurringWord { 5 | 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | // create here the ArrayList 9 | 10 | ArrayList words = new ArrayList(); 11 | String word = "zerowords"; 12 | 13 | while (true) { 14 | System.out.print("Type a word: "); 15 | word = reader.nextLine(); 16 | if (words.contains(word)) { 17 | break; 18 | } else { 19 | words.add(word); 20 | } 21 | } 22 | 23 | System.out.println("You gave the word "+word+" twice"); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Week 3 Exercise 059 Words in Reverse Order/WordsInReverseOrder: -------------------------------------------------------------------------------- 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 | // create here an ArrayList 9 | 10 | ArrayList words = new ArrayList(); 11 | 12 | while (true) { 13 | System.out.print("Type a word: "); 14 | String typed = reader.nextLine(); 15 | if (!(typed.isEmpty())) { 16 | words.add(typed); 17 | } else { 18 | break; 19 | } 20 | } 21 | 22 | System.out.println("You typed the following words: "); 23 | 24 | int count = 0; 25 | while (count < words.size()) { 26 | int word = words.size()-count; 27 | System.out.println(words.get(word-1)); 28 | count++; 29 | } 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Week 3 Exercise 060 Words in Alphabetical Order/WordsInAlphabeticalOrder: -------------------------------------------------------------------------------- 1 | import com.sun.tools.doclets.formats.html.SourceToHTMLConverter; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.Scanner; 6 | 7 | public class WordsInAlphabeticalOrder { 8 | 9 | public static void main(String[] args) { 10 | Scanner reader = new Scanner(System.in); 11 | // create here an ArrayList 12 | 13 | ArrayList words = new ArrayList(); 14 | String typed = "start"; 15 | 16 | while (true) { 17 | if (!(typed.equals(""))) { 18 | System.out.print("Type a word: "); 19 | typed = reader.nextLine(); 20 | words.add(typed); 21 | } else { 22 | break; 23 | } 24 | } 25 | System.out.println("You typed the following words: "); 26 | Collections.sort(words); 27 | 28 | for (String word : words) { 29 | System.out.println(word); 30 | } 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Week 3 Exercise 061 Number of Items/NumberOfItems: -------------------------------------------------------------------------------- 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 | 17 | public static int countItems(ArrayList list) { 18 | int number = list.size(); 19 | return number; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Week 3 Exercise 062 Remove Last/RemoveLast: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | 4 | public class RemoveLast { 5 | public static void removeLast(ArrayList list) { 6 | int lastNumber = list.size()-1; 7 | list.remove(lastNumber); 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 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Week 3 Exercise 063 Sum of Numbers/SumOfNumbers: -------------------------------------------------------------------------------- 1 | public class SumOfNumbers { 2 | public static int sum(ArrayList list) { 3 | int sum = 0; 4 | for (int num : list) { 5 | sum += num; 6 | } 7 | return sum; 8 | } 9 | 10 | public static void main(String[] args) { 11 | ArrayList list = new ArrayList(); 12 | list.add(3); 13 | list.add(2); 14 | list.add(7); 15 | list.add(2); 16 | 17 | System.out.println("The sum: " + sum(list)); 18 | 19 | list.add(10); 20 | 21 | System.out.println("The sum: " + sum(list)); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Week 3 Exercise 064 Average Number/AverageOfNumbers: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class AverageOfNumbers { 4 | 5 | // Copy here the method sum from previous assignment 6 | public static int sum(ArrayList list) { 7 | int sum = 0; 8 | for (int num : list) { 9 | sum+=num; 10 | } 11 | return sum; 12 | } 13 | 14 | 15 | public static double average(ArrayList list) { 16 | double sum = sum(list); 17 | int size = list.size(); 18 | double average = sum/size; 19 | return average; 20 | } 21 | 22 | public static void main(String[] args) { 23 | ArrayList list = new ArrayList(); 24 | 25 | 26 | 27 | 28 | list.add(3); 29 | list.add(2); 30 | list.add(7); 31 | list.add(2); 32 | 33 | System.out.println("The average is: " + average(list)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Week 3 Exercise 065 Length Of Strings/LengthsOfStrings: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class LengthsOfStrings { 4 | public static ArrayList lengths(ArrayList list) { 5 | ArrayList lengthList = new ArrayList(); 6 | 7 | for (String word : list) { 8 | int num = word.length(); 9 | lengthList.add(num); 10 | } 11 | return lengthList; 12 | 13 | } 14 | 15 | public static void main(String[] args) { 16 | ArrayList list = new ArrayList(); 17 | list.add("Ciao"); 18 | list.add("Moi"); 19 | list.add("Benvenuto!"); 20 | list.add("badger badger badger badger"); 21 | ArrayList lengths = lengths(list); 22 | 23 | System.out.println("The lengths of the Strings: " + lengths); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Week 3 Exercise 066 The Greatest/TheGreatest: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class TheGreatest { 4 | public static int greatest(ArrayList list) { 5 | 6 | int greatest = 0; 7 | int count = 0; 8 | for (int number : list) { 9 | if (number < 0 && count==0) { 10 | greatest = number; 11 | } else if (number > greatest) { 12 | greatest = number; 13 | } 14 | count++; 15 | } 16 | return greatest; 17 | } 18 | 19 | public static void main(String[] args) { 20 | ArrayList lista = new ArrayList(); 21 | lista.add(3); 22 | lista.add(2); 23 | lista.add(7); 24 | lista.add(2); 25 | 26 | System.out.println("The greatest number is: " + greatest(lista)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Week 3 Exercise 067 Variance/Variance: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Variance { 4 | 5 | public static int sum(ArrayList list) { 6 | int sum = 0; 7 | for (int number : list) { 8 | sum+=number; 9 | } 10 | return sum; 11 | } 12 | 13 | public static double average(ArrayList list) { 14 | double sum = 0; 15 | int count = 0; 16 | for (int number : list) { 17 | sum+=number; 18 | count++; 19 | } 20 | double average = (sum/count)*1.0; 21 | return average; 22 | } 23 | 24 | 25 | public static double variance(ArrayList list) { 26 | double mean = average(list); 27 | double sumVar = 0.0; 28 | int count = 0; 29 | 30 | for (int number : list) { 31 | double partMean = number-mean; 32 | double square = partMean*partMean; 33 | sumVar+=square; 34 | count++; 35 | } 36 | double variance = (sumVar/(count-1)*1.0); 37 | return variance; 38 | } 39 | 40 | public static void main(String[] args) { 41 | ArrayList list = new ArrayList(); 42 | list.add(3); 43 | list.add(2); 44 | list.add(7); 45 | list.add(2); 46 | 47 | System.out.println("The variance is: " + variance(list)); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Week 3 Exercise 068 NumberMoreThanOnce/MoreThanOnce: -------------------------------------------------------------------------------- 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 | boolean first = false; 8 | 9 | for (int number : list) { 10 | if (!first && number==searched) { 11 | first=true; 12 | } else if (first && number==searched) { 13 | return true; 14 | } 15 | } 16 | return false; 17 | } 18 | 19 | public static void main(String[] args) { 20 | Scanner reader = new Scanner(System.in); 21 | ArrayList list = new ArrayList(); 22 | list.add(3); 23 | list.add(2); 24 | list.add(7); 25 | list.add(2); 26 | 27 | System.out.println("Type a number: "); 28 | int number = Integer.parseInt(reader.nextLine()); 29 | if (moreThanOnce(list, number)) { 30 | System.out.println(number + " appears more than once."); 31 | } else { 32 | System.out.println(number + " does not appear more than once. "); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Week 3 Exercise 069 Palindrome/Palindrome: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Palindromi { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | 8 | System.out.println("Type a text: "); 9 | String text = reader.nextLine(); 10 | if (palindrome(text)) { 11 | System.out.println("The text is a palindrome!"); 12 | } else { 13 | System.out.println("The text is not a palindrome!"); 14 | } 15 | } 16 | 17 | public static boolean palindrome(String text) { 18 | String reversedText = ""; 19 | int num = 0; 20 | 21 | while (num list1 = new ArrayList(); 10 | ArrayList list2 = new ArrayList(); 11 | 12 | Collections.addAll(list1, 4, 3); 13 | // A shorthand for: 14 | // list1.add(4); 15 | // list1.add(3); 16 | 17 | 18 | Collections.addAll(list2, 5, 10, 7); 19 | // A shorthand for: 20 | // list2.add(5); 21 | // list2.add(10); 22 | // list2.add(7); 23 | 24 | // Implement the following method and then remove the comment 25 | // combine(list1, list2); 26 | System.out.println(list1); 27 | System.out.println(list2); 28 | 29 | 30 | } 31 | 32 | public static void combine(ArrayList first, ArrayList second) { 33 | first.addAll(second); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Week 4 Exercise 071 SmartCombining/SmartCombining: -------------------------------------------------------------------------------- 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 first, ArrayList second) { 21 | int count1 = 0; 22 | int count2 = 0; 23 | 24 | for (int num2 : second) { 25 | if (!first.contains(num2)) { 26 | first.add(num2); 27 | } 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Week 4 Exercise 072.1 Your First Account/Account: -------------------------------------------------------------------------------- 1 | public class Account { 2 | 3 | private double balance; 4 | private String owner; 5 | 6 | public Account(String owner, double balance) { 7 | this.balance = balance; 8 | this.owner = owner; 9 | } 10 | 11 | public void deposit(double amount) { 12 | balance += amount; 13 | } 14 | 15 | public void withdrawal(double amount) { 16 | balance -= amount; 17 | } 18 | 19 | public double balance() { 20 | return balance; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return owner + " balance: " + balance; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Week 4 Exercise 072.1 Your First Account/Accounts: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Accounts { 4 | 5 | public static void main(String[] args) { 6 | // Code in Account.Java should not be touched! 7 | // write your code here 8 | 9 | Account account = new Account("New Account", 100.0); 10 | 11 | account.deposit(20.0); 12 | System.out.println(account); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Week 4 Exercise 072.2 YourFirstMoneyTransfer/Account: -------------------------------------------------------------------------------- 1 | public class Account { 2 | 3 | private double balance; 4 | private String owner; 5 | 6 | public Account(String owner, double balance) { 7 | this.balance = balance; 8 | this.owner = owner; 9 | } 10 | 11 | public void deposit(double amount) { 12 | balance += amount; 13 | } 14 | 15 | public void withdrawal(double amount) { 16 | balance -= amount; 17 | } 18 | 19 | public double balance() { 20 | return balance; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return owner + " balance: " + balance; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Week 4 Exercise 072.2 YourFirstMoneyTransfer/Accounts: -------------------------------------------------------------------------------- 1 | public class Accounts { 2 | 3 | public static void main(String[] args) { 4 | // Code in Account.Java should not be touched! 5 | // write your code here 6 | 7 | Account mattAccount = new Account("Matt's account", 1000.0); 8 | Account myAccount = new Account("My account", 0.0); 9 | 10 | mattAccount.withdrawal(100.0); 11 | myAccount.deposit(100.0); 12 | 13 | System.out.println(mattAccount); 14 | System.out.println(myAccount); 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Week 4 Exercise 072.3 Money Transfers/Account: -------------------------------------------------------------------------------- 1 | public class Account { 2 | 3 | private double balance; 4 | private String owner; 5 | 6 | public Account(String owner, double balance) { 7 | this.balance = balance; 8 | this.owner = owner; 9 | } 10 | 11 | public void deposit(double amount) { 12 | balance += amount; 13 | } 14 | 15 | public void withdrawal(double amount) { 16 | balance -= amount; 17 | } 18 | 19 | public double balance() { 20 | return balance; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return owner + " balance: " + balance; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Week 4 Exercise 072.3 Money Transfers/Accounts: -------------------------------------------------------------------------------- 1 | public class Accounts { 2 | 3 | public static void main(String[] args) { 4 | // Code in Account.Java should not be touched! 5 | // write your code here 6 | 7 | Account A = new Account("A", 100.0); 8 | Account B = new Account("B", 0.0); 9 | Account C = new Account("C", 0.0); 10 | 11 | transfer(A, B, 50.0); 12 | transfer(B, C, 25.0); 13 | } 14 | 15 | public static void transfer (Account from, Account to, double howMuch) { 16 | from.withdrawal(howMuch); 17 | to.deposit(howMuch); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Week 4 Exercise 073 Product/Product: -------------------------------------------------------------------------------- 1 | public class Product { 2 | private String name; 3 | private int amount; 4 | private double price; 5 | 6 | public Product (String nameAtStart, double priceAtStart, int amountAtStart) { 7 | this.name = nameAtStart; 8 | this.amount = amountAtStart; 9 | this.price = priceAtStart; 10 | } 11 | 12 | public void printProduct() { 13 | System.out.println(this.name+", price "+this.price+", amount "+this.amount); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Week 4 Exercise 074 Multiplier/Multiplier: -------------------------------------------------------------------------------- 1 | public class Multiplier { 2 | private int myNumber; 3 | 4 | public Multiplier(int number) { 5 | this.myNumber = number; 6 | } 7 | 8 | public int multiply(int otherNumber) { 9 | return otherNumber*this.myNumber; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Week 4 Exercise 075 Decreasing Counter/DecreasingCounter: -------------------------------------------------------------------------------- 1 | public class DecreasingCounter { 2 | private int value; // object variable that remembers the value of counter 3 | private int startValue; // for 75.4 4 | 5 | 6 | public DecreasingCounter(int valueAtStart) { 7 | this.value = valueAtStart; 8 | this.startValue = valueAtStart; 9 | } 10 | 11 | public void printValue() { 12 | System.out.println("value: " + this.value); 13 | } 14 | 15 | public void decrease() { 16 | // write here code to decrement value of counter by one 17 | 18 | if (this.value>0) { // 75.2 19 | this.value--; // 75.1 20 | } 21 | } 22 | // and here the rest of the methods 23 | 24 | public void reset() { // for 75.3 25 | this.value = 0; 26 | } 27 | 28 | public void setInitial() { // for 75.4 29 | this.value=startValue; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Week 4 Exercise 075 Decreasing Counter/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | DecreasingCounter counter = new DecreasingCounter(10); 4 | 5 | counter.printValue(); 6 | counter.decrease(); 7 | counter.decrease(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Week 4 Exercise 076 Menu/Menu: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Menu { 4 | 5 | private ArrayList meals; 6 | 7 | public Menu() { 8 | this.meals = new ArrayList(); 9 | } 10 | // implement methods here 11 | 12 | public void addMeal(String meal) { // 76.1 13 | if (!this.meals.contains(meal)) { 14 | this.meals.add(meal); 15 | } 16 | } 17 | 18 | public void printMeals() { // 76.2 19 | for (String meal : meals) { 20 | System.out.println(meal); 21 | } 22 | } 23 | 24 | public void clearMenu() { // 76.3 25 | meals.clear(); // ---> need to look this up. NetBeans only?? 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Week 4 Exercise 077 Lyyra Card/LyyraCard: -------------------------------------------------------------------------------- 1 | public class LyyraCard { 2 | private double balance; 3 | 4 | public LyyraCard(double balanceAtStart) { 5 | this.balance = balanceAtStart; 6 | } 7 | 8 | public String toString() { 9 | return "The card has " + this.balance + " euros"; 10 | } 11 | 12 | public void payEconomical() { // 77.2 13 | // write code here 14 | 15 | if (this.balance >= 2.50) { // 77.3 16 | this.balance -= 2.50; 17 | } 18 | } 19 | 20 | public void payGourmet() { // 77.2 21 | // write code here 22 | 23 | if (this.balance >= 4.00) { // 77.3 24 | this.balance -= 4.00; 25 | } 26 | } 27 | 28 | public void loadMoney(double amount) { 29 | // write code here 30 | 31 | if (amount >= 0) { 32 | this.balance += amount; 33 | if (this.balance>150.00) { 34 | this.balance = 150.00; 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Week 4 Exercise 077 Lyyra Card/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | LyyraCard pekka = new LyyraCard(20.0); 4 | LyyraCard brian = new LyyraCard(30.0); 5 | 6 | pekka.payGourmet(); 7 | brian.payEconomical(); 8 | 9 | System.out.println("Pekka: " + pekka); 10 | System.out.println("Brian: " + brian); 11 | 12 | pekka.loadMoney(20.0); 13 | brian.payGourmet(); 14 | 15 | System.out.println("Pekka: " + pekka); 16 | System.out.println("Brian: " + brian); 17 | 18 | pekka.payEconomical(); 19 | pekka.payEconomical(); 20 | brian.loadMoney(50.0); 21 | 22 | System.out.println("Pekka: " + pekka); 23 | System.out.println("Brian: " + brian); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Week 4 Exercise 078 Clock Using Counter/BoundedCounter: -------------------------------------------------------------------------------- 1 | public class BoundedCounter { 2 | private int value=0; 3 | private int upperLimit; 4 | 5 | 6 | public BoundedCounter(int upperLimit) { 7 | this.value = 0; 8 | this.upperLimit = upperLimit; 9 | } 10 | 11 | public void next() { 12 | if (this.value=10) { 21 | return "" + this.value; 22 | } else { // if (this.value<10) { 23 | return "0" + this.value; 24 | } 25 | } 26 | 27 | public int getValue() { // might need to change this? No, this seems good. 28 | return this.value; 29 | } 30 | 31 | public void setValue(int value) { 32 | if (value<0 || value>this.upperLimit) { 33 | // "Do nothing if less than 0 or greater than upperLimit" 34 | } else { 35 | this.value = value; 36 | } 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Week 4 Exercise 078 Clock Using Counter/Main: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner reader = new Scanner(System.in); 7 | BoundedCounter seconds = new BoundedCounter(59); 8 | BoundedCounter minutes = new BoundedCounter(59); 9 | BoundedCounter hours = new BoundedCounter(23); 10 | 11 | System.out.print("seconds: "); 12 | int s = Integer.parseInt(reader.nextLine()); 13 | System.out.print("minutes: "); 14 | int m = Integer.parseInt(reader.nextLine()); 15 | System.out.print("hours: "); 16 | int h = Integer.parseInt(reader.nextLine()); 17 | 18 | seconds.setValue(s); 19 | minutes.setValue(m); 20 | hours.setValue(h); 21 | 22 | 23 | for (int i = 0; i < 121; i++) { 24 | System.out.println(hours + ":" + minutes + ":" + seconds); 25 | 26 | seconds.next(); 27 | 28 | if (seconds.getValue()== 0){ // if minutes become zero, advance hours 29 | minutes.next(); 30 | } 31 | 32 | if (minutes.getValue()== 0 && seconds.getValue()== 0){ 33 | hours.next(); 34 | } 35 | 36 | 37 | 38 | } 39 | 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Week 4 Exercise 079 NumberStatistics/Main: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | NumberStatistics stats = new NumberStatistics(); 7 | NumberStatistics statsEven = new NumberStatistics(); 8 | NumberStatistics statsOdd = new NumberStatistics(); 9 | 10 | int userNumber = 0; 11 | 12 | System.out.print("Type numbers: "); 13 | 14 | while (true) { 15 | userNumber = Integer.parseInt(scanner.nextLine()); 16 | if (userNumber==-1) { 17 | break; 18 | } else if (userNumber %2 ==0) { 19 | stats.addNumber(userNumber); 20 | statsEven.addNumber(userNumber); 21 | } else { 22 | stats.addNumber(userNumber); 23 | statsOdd.addNumber(userNumber); 24 | } 25 | } 26 | 27 | System.out.println("sum: " + stats.sum()); 28 | System.out.println("sum of even: " + statsEven.sum()); 29 | System.out.println("sum of odd: " + statsOdd.sum()); 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Week 4 Exercise 079 NumberStatistics/NumberStatistics: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class NumberStatistics { 4 | private int amountOfNumbers; 5 | private ArrayList numbersInput; 6 | 7 | public NumberStatistics() { 8 | this.amountOfNumbers = 0; 9 | this.numbersInput = new ArrayList(); 10 | } 11 | 12 | public void addNumber(int number) { 13 | this.numbersInput.add(number); 14 | this.amountOfNumbers++; 15 | } 16 | 17 | public int amountOfNumbers() { 18 | return this.amountOfNumbers; 19 | } 20 | 21 | public int sum() { 22 | int sum = 0; 23 | 24 | if (this.amountOfNumbers()==0) { 25 | return 0; 26 | } else { 27 | for (int number : this.numbersInput) { 28 | sum+=number; 29 | } 30 | 31 | return sum; 32 | } 33 | } 34 | 35 | public double average() { 36 | double theAverage = 0; 37 | 38 | if (this.amountOfNumbers()==0) { 39 | return 0; 40 | } else { 41 | theAverage = (this.sum()*1.0) / (this.amountOfNumbers()*1.0); 42 | return theAverage; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Week 4 Exercise 080 Rolling the Dice/Dice: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class Dice { 4 | private Random random; 5 | private int numberOfSides; 6 | 7 | public Dice(int numberOfSides) { 8 | this.numberOfSides = numberOfSides; 9 | this.random = new Random(); 10 | } 11 | 12 | public int roll() { 13 | // get random number in range 1-numberOfSides 14 | 15 | return this.random.nextInt(this.numberOfSides)+1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Week 4 Exercise 080 Rolling the Dice/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | 5 | Dice dice = new Dice(6); 6 | 7 | int i = 0; 8 | while (i < 10) { 9 | System.out.println(dice.roll()); 10 | i++; 11 | } 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Week 4 Exercise 081 Password Randomizer/Password Randomizer: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class PasswordRandomizer { 4 | // define the variables here 5 | private int pwLength; 6 | private Random random; 7 | 8 | public PasswordRandomizer(int length) { 9 | // format the variable here 10 | this.pwLength = length; 11 | random = new Random(); 12 | } 13 | 14 | public String createPassword() { 15 | // returns the new password 16 | String newPW = ""; 17 | int nextRandomNum = 0; 18 | 19 | for (int currentPosition = 0; currentPosition < this.pwLength; currentPosition++) { 20 | nextRandomNum = this.random.nextInt(25); 21 | char symbol = "abcdefghijklmnopqrstuvwxyz".charAt(nextRandomNum); 22 | newPW+=symbol; 23 | } 24 | return newPW; 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Week 4 Exercise 081 Password Randomizer/Program: -------------------------------------------------------------------------------- 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 4 Exercise 082 Lottery/LotteryNumbers: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Random; 3 | 4 | 5 | public class LotteryNumbers { 6 | private ArrayList numbers; 7 | private Random random; 8 | 9 | public LotteryNumbers() { 10 | this.random = new Random(); // x7 random numbers between 1 and 39 11 | this.numbers = new ArrayList(); 12 | 13 | this.drawNumbers(); 14 | } 15 | 16 | public ArrayList numbers() { 17 | return this.numbers; 18 | } 19 | 20 | public void drawNumbers() { 21 | // write number drawing here using method containsNumber() 22 | this.numbers.clear(); // clear arraylist first 23 | int currentRando = 0; 24 | int count = 0; 25 | 26 | while (count < 7) { 27 | currentRando = (this.random.nextInt(39)) + 1; 28 | 29 | if (!this.containsNumber(currentRando)) { 30 | this.numbers.add(currentRando); 31 | count++; 32 | } 33 | } 34 | } 35 | 36 | public boolean containsNumber(int number) { 37 | // test if number is already among the drawn numbers 38 | 39 | if (!this.numbers.contains(number)) { 40 | //perform this code if arraylist numbers does not contain int number. 41 | return false; 42 | } else { 43 | return true; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Week 4 Exercise 082 Lottery/Main: -------------------------------------------------------------------------------- 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 4 Exercise 083 Hangman Game Logic/Hangman: -------------------------------------------------------------------------------- 1 | public class HangmanLogic { 2 | 3 | private String word; 4 | private String guessedLetters; 5 | private int numberOfFaults; 6 | 7 | public HangmanLogic(String word) { 8 | this.word = word.toUpperCase(); 9 | this.guessedLetters = ""; 10 | this.numberOfFaults = 0; 11 | } 12 | 13 | public int numberOfFaults() { 14 | return this.numberOfFaults; 15 | } 16 | 17 | public String guessedLetters() { 18 | return this.guessedLetters; 19 | } 20 | 21 | public int losingFaultAmount() { 22 | return 12; 23 | } 24 | 25 | public void guessLetter(String letter) { // 83.1 26 | // program here the functionality for making a guess 27 | // if the letter has already been guessed, nothing happens 28 | //i: if the letter has been guessed, it does not add letter to guessedLetters variable. 29 | 30 | // if the word does not contain the guessed letter, number of faults increases 31 | //i: increase number of faults only if... letter is not already guessed and letter is not in word 32 | //i: ((at end if none of the other if statements are true)) 33 | // the letter is added among the already guessed letters. 34 | //i: always add letter to already guessed list, unless it was guessed before this method is called. 35 | 36 | if (guessedLetters.contains(letter)) { 37 | // if it is already guessed, do nothing. 38 | } else if (word.contains(letter)) { 39 | guessedLetters += letter; //add guessed letter to guessedLetters variable if letter is not already guessed. 40 | } else { 41 | numberOfFaults++; 42 | guessedLetters += letter; 43 | } 44 | } 45 | 46 | public String hiddenWord() { 47 | // program here the functionality for building the hidden word 48 | // create the hidden word by iterating through this.word letter by letter 49 | // if the letter in question, from this.word, is within this.guessedLetters, put it in the hidden word 50 | // if the letter in question, from this.word, is not among guessedLetters, replace it with _ in the hidden word 51 | //i: run thru each letter in this.word and use guessedLetters.contains(letter) to see if current letter has been guessed. 52 | //i: if so, add it to hiddenWord. if not, add "_" to hiddenWord. then move onto the next letter 53 | 54 | 55 | // return the hidden word at the end 56 | int wordLength = word.length(); 57 | String hiddenWord = ""; 58 | 59 | for (int count = 0; count < this.word.length(); count++) { 60 | char thisLetter = this.word.charAt(count); 61 | String stringLetter = "" + thisLetter; 62 | 63 | if (guessedLetters.contains(stringLetter)) { 64 | hiddenWord += thisLetter; 65 | } else { 66 | hiddenWord += "_"; 67 | } 68 | } 69 | return hiddenWord; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Week 4 Exercise 083 Hangman Game Logic/Main: -------------------------------------------------------------------------------- 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 Exercise 084 Overloaded Counter/Counter: -------------------------------------------------------------------------------- 1 | public class Counter { 2 | public int startingValue; 3 | public int counter; 4 | public boolean check; 5 | 6 | public Counter() { 7 | this(0, false); 8 | } 9 | 10 | public Counter(boolean check) { 11 | this(0, check); 12 | } 13 | 14 | public Counter(int startingValue) { 15 | this(startingValue, false); 16 | } 17 | 18 | public Counter(int startingValue, boolean check) { 19 | this.startingValue = startingValue; 20 | this.check = check; 21 | this.counter = startingValue; 22 | } 23 | 24 | public int value() { 25 | return this.counter; 26 | } 27 | 28 | public void increase() { 29 | this.increase(1); 30 | } 31 | 32 | public void increase(int increaseAmount) { 33 | if (increaseAmount<0) { 34 | //do nothing if variable less than 0 35 | } else { 36 | this.counter+=increaseAmount; 37 | } 38 | } 39 | 40 | public void decrease() { 41 | this.decrease(1); 42 | } 43 | 44 | public void decrease(int decreaseAmount) { 45 | if (decreaseAmount<0) { 46 | //do nothing if variable less than 0 47 | } else { 48 | if (this.check) { 49 | this.counter-=decreaseAmount; 50 | if (this.counter<0) { //if check is on, can't go below 0 51 | this.counter = 0; 52 | } 53 | } else { 54 | this.counter-=decreaseAmount; //if check off, do no matter what 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Week 5 Exercise 085 Reformatory/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | Reformatory eastHelsinkiReformatory = new Reformatory(); 5 | 6 | Person brian = new Person("Brian", 1, 110, 7); 7 | Person pekka = new Person("Pekka", 33, 176, 85); 8 | 9 | System.out.println(brian.getName() + " weight: " + eastHelsinkiReformatory.weight(brian) + " kilos"); 10 | System.out.println(pekka.getName() + " weight: " + eastHelsinkiReformatory.weight(pekka) + " kilos"); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Week 5 Exercise 085 Reformatory/Person: -------------------------------------------------------------------------------- 1 | public class Person { 2 | 3 | private String name; 4 | private int age; 5 | private int height; 6 | private int weight; 7 | 8 | public Person(String name, int age, int height, int weight) { 9 | this.name = name; 10 | this.age = age; 11 | this.height = height; 12 | this.weight = weight; 13 | } 14 | 15 | public void becomeOlder() { 16 | this.age++; 17 | } 18 | 19 | public boolean adult() { 20 | if (this.age < 18) { 21 | return false; 22 | } 23 | 24 | return true; 25 | } 26 | 27 | public double weightIndex() { 28 | double heightInMeters = this.height / 100.0; 29 | 30 | return this.weight / (heightInMeters * heightInMeters); 31 | } 32 | 33 | public void setHeight(int height) { 34 | this.height = height; 35 | } 36 | 37 | public int getHeight() { 38 | return this.height; 39 | } 40 | 41 | public int getWeight() { 42 | return this.weight; 43 | } 44 | 45 | public void setWeight(int weight) { 46 | this.weight = weight; 47 | } 48 | 49 | public String getName() { 50 | return this.name; 51 | } 52 | 53 | public String toString() { 54 | return this.name + " I am " + this.age + " years old, my weight index is " + this.weightIndex(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Week 5 Exercise 085 Reformatory/Reformatory: -------------------------------------------------------------------------------- 1 | public class Reformatory { 2 | 3 | private int timesWeighed; //85.3 4 | 5 | public Reformatory() { 6 | 7 | this.timesWeighed = 0; 8 | } 9 | 10 | public int weight(Person person) { //85.1 11 | // returns the weight of the parameter 12 | 13 | this.timesWeighed++; //85.3 14 | return person.getWeight(); 15 | } 16 | 17 | public void feed(Person person) { //85,2 18 | person.setWeight(person.getWeight()+1); 19 | } 20 | 21 | public int totalWeightsMeasured() { 22 | return timesWeighed; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Week 5 Exercise 086 Lyyra Card and Cash Register/CashRegister: -------------------------------------------------------------------------------- 1 | public class CashRegister { //86.2 2 | private double cashInRegister; // the amount of cash in the register 3 | private int economicalSold; // the amount of economical lunches sold 4 | private int gourmetSold; // the amount of gourmet lunches sold 5 | 6 | public CashRegister() { 7 | // at start the register has 1000 euros 8 | 9 | this.cashInRegister = 1000; 10 | this.economicalSold = 0; 11 | this.gourmetSold = 0; 12 | } 13 | 14 | public double payEconomical(double cashGiven) { // PAY WITH CASH 15 | // the price of the economical lunch is 2.50 euros 16 | // if the given cash is at least the price of the lunch: 17 | // the price of lunch is added to register 18 | // the amount of the sold lunches is incremented by one 19 | // the method returns cashGiven - lunch price 20 | // if not enough money is given, all is returned and nothing else happens 21 | 22 | if (cashGiven >= 2.50) { 23 | this.cashInRegister += 2.50; 24 | this.economicalSold++; 25 | return cashGiven - 2.50; 26 | } else { 27 | return cashGiven; 28 | } 29 | } 30 | 31 | public double payGourmet(double cashGiven) { // PAY WITH CASH 32 | // the price of the gourmet lunch is 4.00 euros 33 | // if the given cash is at least the price of the lunch: 34 | // the price of lunch is added to the register 35 | // the amount of the sold lunches is incremented by one 36 | // the method returns cashGiven - lunch price 37 | // if not enough money is given, all is returned and nothing else happens 38 | 39 | if (cashGiven >= 4.00) { 40 | this.cashInRegister += 4.00; 41 | this.gourmetSold++; 42 | return cashGiven - 4.00; 43 | } else { 44 | return cashGiven; 45 | } 46 | } 47 | 48 | public String toString() { 49 | return "money in register " + cashInRegister + " economical lunches sold: " + economicalSold + " gourmet lunches sold: " + gourmetSold; 50 | } 51 | 52 | public boolean payEconomical(LyyraCard card) { //86.3 53 | // the price of the economical lunch is 2.50 euros 54 | // if the balance of the card is at least the price of the lunch: 55 | // the amount of sold lunches is incremented by one 56 | // the method returns true 57 | // if not, the method returns false 58 | 59 | boolean canPay = card.pay(2.50); 60 | 61 | if (canPay) { 62 | this.economicalSold++; 63 | return true; 64 | } else { 65 | return false; 66 | } 67 | } 68 | 69 | public boolean payGourmet(LyyraCard card) { //86.3 70 | // the price of the gourmet lunch is 4.00 euros 71 | // if the balance of the card is at least the price of the lunch: 72 | // the amount of sold lunches is incremented by one 73 | // the method returns true 74 | // if not, the method returns false 75 | 76 | boolean canPay = card.pay(4.00); 77 | 78 | if (canPay) { 79 | this.gourmetSold++; 80 | return true; 81 | } else { 82 | return false; 83 | } 84 | } 85 | 86 | public void loadMoneyToCard(LyyraCard card, double sum) { //86.4 - too simple? 87 | if (sum > 0) { 88 | card.loadMoney(sum); 89 | this.cashInRegister += sum; 90 | } 91 | 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /Week 5 Exercise 086 Lyyra Card and Cash Register/Lyyra Card: -------------------------------------------------------------------------------- 1 | public class LyyraCard { 2 | private double balance; 3 | 4 | public LyyraCard(double balance) { 5 | this.balance = balance; 6 | } 7 | 8 | public double balance() { 9 | return this.balance; 10 | } 11 | 12 | public void loadMoney(double amount) { 13 | this.balance += amount; 14 | } 15 | 16 | public boolean pay(double amount){ //86.1 17 | // the method checks if the balance of the card is at least the amount given as parameter 18 | // if not, the method returns false meaning that the card could not be used for the payment 19 | // if the balance is enough, the given amount is taken from the balance and true is returned 20 | 21 | if (this.balance>=amount) { 22 | this.balance-=amount; 23 | return true; 24 | } else { 25 | return false; 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Week 5 Exercise 087 Apartment Comparison/Apartment: -------------------------------------------------------------------------------- 1 | public class Apartment { 2 | private int rooms; 3 | private int squareMeters; 4 | private int pricePerSquareMeter; 5 | 6 | public Apartment(int rooms, int squareMeters, int pricePerSquareMeter){ 7 | this.rooms = rooms; 8 | this.squareMeters = squareMeters; 9 | this.pricePerSquareMeter = pricePerSquareMeter; 10 | } 11 | 12 | public boolean larger(Apartment otherApartment) { //87.1 13 | if (this.squareMeters > otherApartment.squareMeters) { 14 | return true; 15 | } 16 | return false; 17 | } 18 | 19 | public int priceDifference(Apartment otherApartment) { //87.2 20 | int thisPrice = this.squareMeters*this.pricePerSquareMeter; 21 | int thatPrice = otherApartment.squareMeters*otherApartment.pricePerSquareMeter; 22 | 23 | if (thisPrice>=thatPrice) { 24 | return thisPrice-thatPrice; 25 | } else { 26 | return thatPrice-thisPrice; 27 | } 28 | } 29 | 30 | public boolean moreExpensiveThan(Apartment otherApartment) { //87.3 31 | int thisPrice = this.squareMeters*this.pricePerSquareMeter; 32 | int thatPrice = otherApartment.squareMeters*otherApartment.pricePerSquareMeter; 33 | 34 | if (thisPrice > thatPrice) { 35 | return true; 36 | } 37 | return false; 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Week 5 Exercise 087 Apartment Comparison/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // write testcode here 5 | Apartment studioManhattan = new Apartment(1, 16, 5500); 6 | Apartment twoRoomsBrooklyn = new Apartment(2, 38, 4200); 7 | Apartment fourAndKitchenBronx = new Apartment(3, 78, 2500); 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Week 5 Exercise 088 Students/Main: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | public static void main(String[] args) { //this main is 88.2 6 | Scanner scanner = new Scanner(System.in); 7 | ArrayList studentList = new ArrayList(); 8 | 9 | while (true) { 10 | System.out.print("name: "); 11 | String name = scanner.nextLine(); 12 | 13 | if (!name.equals("")) { 14 | System.out.print("studentnumber: "); 15 | String studentNumber = scanner.nextLine(); 16 | 17 | studentList.add(new Student(name, studentNumber)); 18 | } else { 19 | System.out.println(""); 20 | for (Student student : studentList) { 21 | System.out.println(student); 22 | } 23 | break; 24 | } 25 | } 26 | 27 | System.out.println(""); // 88.3 28 | System.out.print("Give search term: "); 29 | String search = scanner.nextLine(); 30 | 31 | System.out.println("Result: "); 32 | 33 | for (Student student : studentList) { 34 | String name = student.getName(); 35 | 36 | if (name.contains(search)) { 37 | System.out.println(student); 38 | } 39 | } 40 | 41 | 42 | 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Week 5 Exercise 088 Students/Student: -------------------------------------------------------------------------------- 1 | public class Student { //88.1 2 | private String name; 3 | private String studentNumber; 4 | 5 | public Student(String name, String studentNumber) { 6 | this.name = name; 7 | this.studentNumber = studentNumber; 8 | } 9 | 10 | public String getName() { 11 | return this.name; 12 | } 13 | 14 | public String getStudentNumber() { 15 | return this.studentNumber; 16 | } 17 | 18 | public String toString() { 19 | return this.name+ " (" +this.studentNumber+ ") "; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Week 5 Exercise 089 Clock Object/BoundedCounter: -------------------------------------------------------------------------------- 1 | public class BoundedCounter { 2 | private int value; 3 | private int upperLimit; 4 | 5 | public BoundedCounter(int upperLimit) { 6 | this.value = 0; 7 | this.upperLimit = upperLimit; 8 | } 9 | 10 | public BoundedCounter(int upperLimit, int value) { 11 | this.value=value; 12 | this.upperLimit=upperLimit; 13 | } 14 | 15 | public void next() { 16 | this.value++; 17 | if (this.value >= this.upperLimit) { 18 | this.value = 0; 19 | } 20 | } 21 | 22 | public String toString() { 23 | // 78.1 example: return this.name + ", age " + this.age + " years"; 24 | 25 | if (this.value<10) { 26 | return "0" + this.value; 27 | } else 28 | return "" + this.value; 29 | } 30 | 31 | public int getValue() { 32 | // what does this do? implement how? return int value for comparisons? 33 | 34 | return this.value; 35 | } 36 | 37 | public void setValue(int value) { 38 | // set the value of parameter to counter unless parameterupperLimit. (no effect there) 39 | 40 | if (value<0 || value>this.upperLimit) { 41 | // do nothing 42 | } else { 43 | this.value = value; 44 | } 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Week 5 Exercise 089 Clock Object/Clock: -------------------------------------------------------------------------------- 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 | // the counters that represent hours, minutes and seconds are created and 8 | // set to have the correct initial values 9 | this.hours = new BoundedCounter(24, hoursAtBeginning); 10 | this.minutes = new BoundedCounter(60, minutesAtBeginning); 11 | this.seconds = new BoundedCounter(60, secondsAtBeginning); 12 | 13 | /* Do I need to make sure values>-1 && values teamPlayers = new ArrayList(); 5 | private String teamName; 6 | private int maxTeamSize; 7 | 8 | 9 | public Team(String name) { 10 | this.teamName=name; 11 | this.maxTeamSize = 16; 12 | } 13 | 14 | public String getName() { 15 | return this.teamName; 16 | } 17 | 18 | public void addPlayer(Player player) { 19 | 20 | if (this.teamPlayers.size()>=this.maxTeamSize) { 21 | // do not add if maxTeamSize has been reached 22 | } else { 23 | this.teamPlayers.add(player); //add the player to the arraylist 24 | } 25 | } 26 | 27 | public void printPlayers() { 28 | System.out.println("Team: "+this.teamName); 29 | 30 | for (Player thisPlayer : this.teamPlayers) { 31 | System.out.println("Player: "+thisPlayer); 32 | } 33 | } 34 | 35 | public void setMaxSize(int maxSize) { 36 | this.maxTeamSize = maxSize; 37 | } 38 | 39 | public int size() { 40 | return this.teamPlayers.size(); 41 | } 42 | 43 | public int goals() { 44 | int numberOfGoals = 0; 45 | 46 | for (Player player : teamPlayers) { 47 | numberOfGoals+=player.goals(); 48 | } 49 | 50 | return numberOfGoals; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Week 5 Exercise 091 Extending MyDate/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // test your code here 5 | MyDate myDate = new MyDate(1,7,2017); 6 | 7 | System.out.println("Start Date: "+myDate); 8 | myDate.advance(); 9 | System.out.println("After one day: "+myDate); 10 | 11 | int count = 0; 12 | while (count<30) { 13 | myDate.advance(); 14 | System.out.println(myDate); 15 | count++; 16 | } 17 | 18 | myDate.advance(); 19 | System.out.println("Final Date: "+myDate); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Week 5 Exercise 091 Extending MyDate/MyDate: -------------------------------------------------------------------------------- 1 | public class MyDate { 2 | 3 | private int day; 4 | private int month; 5 | private int year; 6 | 7 | public MyDate(int day, int month, int year) { 8 | this.day = day; 9 | this.month = month; 10 | this.year = year; 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 | return false; 31 | } 32 | 33 | public void advance() { // 91.1 34 | 35 | this.day++; 36 | if (this.day>30) { 37 | this.day=1; 38 | this.month++; 39 | if (this.month>12) { 40 | this.month=1; 41 | this.year++; 42 | } 43 | } 44 | } 45 | 46 | public void advance(int numberOfDays) { // 91.2 47 | 48 | int counter = 1; 49 | 50 | while (counter<=numberOfDays) { 51 | this.advance(); 52 | counter++; 53 | } 54 | } 55 | 56 | public MyDate afterNumberOfDays(int days) { //91.3 57 | MyDate newMyDate = new MyDate(this.day, this.month, this.year); // assign newMyDate date to the same as this object 58 | newMyDate.advance(days); // advance the days 59 | 60 | return newMyDate; // return the new object 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Week 5 Exercise 092 Difference of Two Names/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // write testcode here 5 | MyDate myDate = new MyDate(7,1,2017); 6 | MyDate comparedDate = new MyDate(1,12,2017); 7 | 8 | System.out.println("Today's date: "+myDate); 9 | System.out.println("Other date: "+comparedDate); 10 | System.out.println("Difference in years: "+myDate.differenceInYears(comparedDate)); 11 | System.out.println("Is "+myDate+" earlier than "+comparedDate+"? "+myDate.earlier(comparedDate)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Week 5 Exercise 092 Difference of Two Names/MyDate: -------------------------------------------------------------------------------- 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) { // 92.1 -- will this work with private variables like comparedDate.year ?? 34 | 35 | int totalDays1 = this.day+(this.month*30)+(this.year*365); 36 | int totalDays2 = comparedDate.day+(comparedDate.month*30)+(comparedDate.year*365); 37 | 38 | int daysDifferent = 0; 39 | if (totalDays1==totalDays2) { 40 | return 0; 41 | } else if (totalDays1 > totalDays2) { // 92.3 42 | daysDifferent = totalDays1 - totalDays2; 43 | } else { 44 | daysDifferent = totalDays2 - totalDays1; 45 | } 46 | 47 | 48 | return (int) daysDifferent / 365; // revamped 92.2 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Week 5 Exercise 093 Person Extended/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | // write test code here 4 | // Person pekka = new Person("Pekka", 15, 2, 1993); 5 | Person pekka = new Person("Pekka", new MyDate(31,12,2009)); 6 | Person steve = new Person("Steve"); 7 | Person chris = new Person("Christopher", new MyDate(1,1,2010)); 8 | 9 | System.out.println( pekka ); 10 | System.out.println( steve ); 11 | 12 | System.out.println(); 13 | System.out.println("Who's older? "); 14 | boolean P; 15 | 16 | if (pekka.olderThan(steve)) { 17 | P = true; 18 | System.out.println(pekka); 19 | } else { 20 | System.out.println(steve); 21 | P = false; 22 | } 23 | 24 | if (P) { 25 | System.out.println(chris); 26 | System.out.println(); 27 | System.out.println("Who's older? "); 28 | 29 | boolean second; 30 | if (pekka.olderThan(chris)) { 31 | second = true; 32 | System.out.println(pekka); 33 | } else { 34 | System.out.println(chris); 35 | second = false; 36 | } 37 | } else { 38 | System.out.println(chris); 39 | System.out.println("Who's older? "); 40 | 41 | boolean second; 42 | if (steve.olderThan(chris)) { 43 | second = true; 44 | System.out.println(steve); 45 | } else { 46 | System.out.println(chris); 47 | second = false; 48 | } 49 | } 50 | 51 | /* 52 | Person chris = new Person ("Christopher", 28,11,1986); 53 | Person pekka = new Person("Pekka", 15, 2, 1993); 54 | Person steve = new Person("Thomas", 1, 3, 1955); 55 | 56 | System.out.println( steve.getName() + " age " + steve.age() + " years"); 57 | System.out.println( pekka.getName() + " age " + pekka.age() + " years"); 58 | System.out.println( chris.getName() + " age " + chris.age() + " years"); 59 | 60 | System.out.println(); 61 | System.out.println("Who's older? "+chris.getName()+" or "+pekka.getName()+"?"); 62 | if (chris.olderThan(pekka)) { 63 | System.out.println(chris.getName()); 64 | } else { 65 | System.out.println(pekka); 66 | } 67 | }*/ 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Week 5 Exercise 093 Person Extended/MyDate: -------------------------------------------------------------------------------- 1 | public class MyDate { 2 | 3 | private int day; 4 | private int month; 5 | private int year; 6 | 7 | public MyDate(int day, int month, int year) { 8 | this.day = day; 9 | this.month = month; 10 | this.year = year; 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 | if (this.year == compared.year && this.month < compared.month) { 22 | return true; 23 | } 24 | if (this.year == compared.year && this.month == compared.month 25 | && this.day < compared.day) { 26 | return true; 27 | } 28 | return false; 29 | } 30 | 31 | /* 32 | * In assignment 92 method differneceInYears was added to MyDate 33 | * Copy the method here since it eases this assignment considerably. 34 | */ 35 | public int differenceInYears(MyDate comparedDate) { // 92.1 -- will this work with private variables like comparedDate.year ?? 36 | 37 | int totalDays1 = this.day+(this.month*30)+(this.year*365); 38 | int totalDays2 = comparedDate.day+(comparedDate.month*30)+(comparedDate.year*365); 39 | 40 | int daysDifferent = 0; 41 | if (totalDays1==totalDays2) { 42 | return 0; 43 | } else if (totalDays1 > totalDays2) { // 92.3 44 | daysDifferent = totalDays1 - totalDays2; 45 | } else { 46 | daysDifferent = totalDays2 - totalDays1; 47 | } 48 | 49 | 50 | return (int) daysDifferent / 365; // revamped 92.2 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Week 5 Exercise 093 Person Extended/Person: -------------------------------------------------------------------------------- 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 day, int month, int year) { 8 | this.name = name; 9 | this.birthday = new MyDate(day, month, year); 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 = name; 19 | int todaysDay = Calendar.getInstance().get(Calendar.DATE); 20 | int todaysMonth = Calendar.getInstance().get(Calendar.MONTH) + 1; // January is 0 so we add one 21 | int todaysYear = Calendar.getInstance().get(Calendar.YEAR); 22 | this.birthday = new MyDate(todaysDay,todaysMonth,todaysYear); 23 | } 24 | 25 | public int age() { 26 | // calculate the age based on the birthday and the current day 27 | // you get the current day as follows: 28 | int todaysDay = Calendar.getInstance().get(Calendar.DATE); 29 | int todaysMonth = Calendar.getInstance().get(Calendar.MONTH) + 1; // January is 0 so we add one 30 | int todaysYear = Calendar.getInstance().get(Calendar.YEAR); 31 | MyDate todaysDate = new MyDate(todaysDay, todaysMonth, todaysYear); 32 | 33 | int compareNumOfDays = this.birthday.differenceInYears(todaysDate); 34 | 35 | return compareNumOfDays; 36 | } 37 | 38 | public boolean olderThan(Person compared) { 39 | // compare the ages based on birthdays 40 | if (this.birthday.earlier(compared.birthday)) { //had to look this up, very difficult at 2am! 41 | return true; 42 | } else { 43 | return false; 44 | } 45 | } 46 | 47 | public String getName() { 48 | return this.name; 49 | } 50 | 51 | public String toString() { 52 | return this.name + ", born " + this.birthday; 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Week 6 Exercise 094 Phone Book/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // write testcode here 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 6 Exercise 094 Phone Book/Person: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private String personName; 3 | private String personNumber; 4 | 5 | public Person(String name, String number) { 6 | this.personName = name; 7 | this.personNumber = number; 8 | } 9 | 10 | public String toString() { 11 | return this.personName + " number: " + this.personNumber; 12 | } 13 | 14 | public String getName() { 15 | return this.personName; 16 | } 17 | 18 | public String getNumber() { 19 | return this.personNumber; 20 | } 21 | 22 | public void changeNumber(String newNumber) { 23 | this.personNumber = newNumber; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Week 6 Exercise 094 Phone Book/Phonebook: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | 4 | public class Phonebook { 5 | private ArrayList phonebook; 6 | 7 | public Phonebook() { 8 | phonebook = new ArrayList(); 9 | } 10 | 11 | public void add(String name, String number) { 12 | Person person = new Person(name, number); 13 | this.phonebook.add(person); 14 | } 15 | 16 | public void printAll() { 17 | for (Person person : phonebook) { 18 | System.out.println(person); 19 | } 20 | } 21 | 22 | public String searchNumber(String name) { 23 | for (Person person : phonebook) { 24 | if (person.getName().equals(name)) { 25 | return person.getNumber(); 26 | } 27 | } 28 | return "number not known"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Week 6 Exercise 095 Money/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // Test your code here! 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 6 Exercise 095 Money/Money: -------------------------------------------------------------------------------- 1 | public class Money { 2 | 3 | private final int euros; 4 | private final int cents; 5 | 6 | public Money(int euros, int cents) { 7 | 8 | if (cents > 99) { 9 | euros += cents / 100; 10 | cents %= 100; 11 | } 12 | 13 | this.euros = euros; 14 | this.cents = cents; 15 | } 16 | 17 | public int euros() { 18 | return euros; 19 | } 20 | 21 | public int cents() { 22 | return cents; 23 | } 24 | 25 | @Override 26 | public String toString() { 27 | String zero = ""; 28 | if (cents < 10) { 29 | zero = "0"; 30 | } 31 | 32 | return euros + "." + zero + cents + "e"; 33 | } 34 | 35 | public Money plus(Money added) { 36 | Money newMoney = new Money((added.euros()+this.euros()), (added.cents()+this.cents())); 37 | return newMoney; 38 | } 39 | 40 | public boolean less(Money compared) { 41 | if (this.euros() < compared.euros()) { 42 | return true; 43 | } else if (this.euros() == compared.euros() && this.cents() < compared.cents()) { 44 | return true; 45 | } else { 46 | return false; 47 | } 48 | } 49 | 50 | public Money minus(Money decremented) { 51 | int newEuros = this.euros()-decremented.euros(); 52 | int newCents = this.cents()-decremented.cents(); 53 | 54 | if (newEuros<0 && newCents<0) { 55 | newEuros = 0; 56 | newCents = 0; 57 | } else if (newEuros<0) { 58 | newEuros = 0; 59 | newCents = 0; 60 | } else if (newCents<0) { 61 | newCents = 100+newCents; 62 | newEuros-=1; 63 | } 64 | 65 | Money newMoney = new Money(newEuros, newCents); 66 | 67 | return newMoney; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Week 6 Exercise 096 Sum of Array/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // test method here 5 | int[] array = {5, 1, 3, 4, 2}; 6 | System.out.println(sum(array)); 7 | } 8 | 9 | public static int sum(int[] array) { 10 | int sum = 0; 11 | 12 | for (int number : array) { 13 | sum+=number; 14 | } 15 | 16 | return sum; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Week 6 Exercise 097 Elegant Printing of an Array/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // test method here 5 | int[] array = {5, 1, 3, 4, 2}; 6 | printElegantly(array); 7 | } 8 | 9 | public static void printElegantly(int[] array) { 10 | // separate with comma and whitespace, no comma trailing at end of last number. 11 | 12 | for (int count = 0; count < array.length; count++) { 13 | 14 | System.out.print(array[count]); 15 | 16 | if (count == array.length-1) { 17 | // do nothing at end 18 | } else { 19 | System.out.print(", "); 20 | } 21 | } 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Week 6 Exercise 098 Copy and Reverse an Array/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // write testcode here 5 | } 6 | 7 | public static int[] copy(int[] array) { 8 | int[] newArray = new int[array.length]; 9 | 10 | for (int counter = 0; counter < array.length; counter++){ 11 | newArray[counter] = array[counter]; 12 | } 13 | return newArray; 14 | } 15 | 16 | public static int[] reverseCopy(int[] array) { 17 | int[] newArray = new int[array.length]; 18 | 19 | int secondCounter = 0; 20 | for (int counter = array.length - 1; counter > -1; counter--){ 21 | newArray[secondCounter] = array[counter]; 22 | secondCounter++; 23 | } 24 | return newArray; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Week 6 Exercise 099 Array As Stars/Main: -------------------------------------------------------------------------------- 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) { // do we need "void" here? do we need "static" ? 10 | 11 | for (int number : array) { // each number of the array 12 | for (int counter = 0; counter < number; counter++) { // counter counts to current number 13 | System.out.print("*"); // print a number of stars equal to "number" variable 14 | } 15 | System.out.println(""); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Week 6 Exercise 100 Nightsky/Main: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner input = new Scanner(System.in); 7 | NightSky night; 8 | 9 | System.out.println("How wide?"); 10 | int width = Integer.parseInt(input.nextLine()); 11 | 12 | System.out.println("How high?"); 13 | int height = Integer.parseInt(input.nextLine()); 14 | 15 | System.out.println("What density percentage?"); 16 | double density = Double.parseDouble(input.nextLine()); 17 | 18 | night = new NightSky(density, width, height); 19 | night.print(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Week 6 Exercise 100 Nightsky/NightSky: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class NightSky { // do we need "void" and "static" ? 4 | private double skyDensity; 5 | private int skyWidth; 6 | private int skyHeight; 7 | private int starsInLastPrint; 8 | 9 | public NightSky(double density) { 10 | this(density, 20, 10); 11 | } 12 | 13 | public NightSky(int width, int height) { 14 | this(0.1, width, height); 15 | } 16 | 17 | public NightSky(double density, int width, int height) { 18 | this.skyDensity = density; 19 | this.skyWidth = width; 20 | this.skyHeight = height; 21 | this.starsInLastPrint = 0; 22 | } 23 | 24 | public void printLine() { 25 | 26 | for (int counter = 0; counter < this.skyWidth; counter++){ // loop it this.skyWidth many times 27 | Random random = new Random(); 28 | double randNum = random.nextDouble(); 29 | 30 | if (randNum <= this.skyDensity) { // if random is at or below this.skyDensity, print a star 31 | 32 | System.out.print("*"); 33 | this.starsInLastPrint++; // 100.3: count a star when you print a star on this.starsInLastPrint 34 | } else { 35 | System.out.print(" "); // white space 36 | } 37 | } 38 | } 39 | 40 | public void print() { // 100.2 41 | this.starsInLastPrint = 0; 42 | 43 | for (int countHigh = 0; countHigh < this.skyHeight; countHigh++) { 44 | this.printLine(); 45 | System.out.println(""); 46 | } 47 | } 48 | 49 | public int starsInLastPrint() { // 100.3 50 | return this.starsInLastPrint; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Week 6 Exercise 101 Library System/Book: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Book { // single book 4 | private String bookTitle; 5 | private String bookPublisher; 6 | private int bookYear; 7 | 8 | public Book(String title, String publisher, int year) { 9 | this.bookTitle = title; 10 | this.bookPublisher = publisher; 11 | this.bookYear = year; 12 | } 13 | 14 | public String title() { 15 | return this.bookTitle; 16 | } 17 | 18 | public String publisher() { 19 | return this.bookPublisher; 20 | } 21 | 22 | public int year() { 23 | return this.bookYear; 24 | } 25 | 26 | public String toString() { 27 | return this.bookTitle + ", " + this.bookPublisher + ", " + this.bookYear + ""; // last "" might not be necessary 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Week 6 Exercise 101 Library System/Library: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Library { // holds set of books and provides various ways to search for the books within 4 | private ArrayList myLibrary; // create arraylist to hold books, with infinite spaces for the books 5 | 6 | public Library() { // constructor 7 | this.myLibrary = new ArrayList(); 8 | } 9 | 10 | public void addBook(Book newBook) { 11 | this.myLibrary.add(newBook); // add newBook to the arraylist. is newBook's parameters title/publisher/year required here? do we need String and int before the parameters? 12 | } 13 | 14 | public void printBooks() { 15 | for (Book printBook : this.myLibrary) { // for each book of myLibrary, print 16 | System.out.println(printBook); 17 | } 18 | } 19 | 20 | public ArrayList searchByTitle(String title) { 21 | ArrayList found = new ArrayList(); 22 | 23 | // iterate the list of books and all the matching books found in the list. 24 | // use method contains instead of equals for String objects 25 | 26 | for (Book findTitle : this.myLibrary) { 27 | if (StringUtils.included(findTitle.title(), title)) { // new if 28 | found.add(findTitle); 29 | } 30 | } 31 | 32 | //new if statement: if (StringUtils.included(findTitle.title(), title)) { 33 | //old if statement: if (findTitle.title().contains(title)) { 34 | 35 | return found; 36 | } 37 | 38 | public ArrayList searchByPublisher(String publisher) { 39 | ArrayList found = new ArrayList(); 40 | 41 | for (Book findPublisher : this.myLibrary) { 42 | if (StringUtils.included(findPublisher.publisher(), publisher)) { // new if 43 | found.add(findPublisher); 44 | } 45 | } 46 | return found; 47 | } 48 | 49 | public ArrayList searchByYear(int year) { 50 | ArrayList found = new ArrayList(); 51 | 52 | for (Book findYear : this.myLibrary) { 53 | if (findYear.year() == year) { 54 | found.add(findYear); 55 | } 56 | } 57 | return found; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Week 6 Exercise 101 Library System/String Utils: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | 4 | public class StringUtils { // 101.4 improved search 5 | 6 | 7 | public StringUtils() { // constructor 8 | 9 | } 10 | 11 | public static boolean included(String word, String searched) { 12 | if (word == null || searched == null || word.isEmpty() || searched.isEmpty()) { 13 | return false; 14 | } 15 | 16 | word = word.trim(); 17 | word = word.toUpperCase(); 18 | searched = searched.trim(); 19 | searched = searched.toUpperCase(); 20 | 21 | if (word.contains(searched)) { 22 | return true; 23 | } else { 24 | return false; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Week 6 Exercise 102 Grade Distribution/Grades: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Grades { 4 | private int[] gradeArray; 5 | 6 | public Grades(Scanner input) { 7 | this.gradeArray = new int[6]; 8 | this.setToZero(); 9 | } 10 | 11 | public void setToZero() { 12 | for (int count = 0; count <= 5; count++) { 13 | this.gradeArray[count] = 0; 14 | } 15 | } 16 | 17 | public void getGrades(Scanner input) { 18 | 19 | while (true) { 20 | int grade = Integer.parseInt(input.nextLine()); 21 | 22 | if (grade == -1) { 23 | System.out.println(""); // go to next line before printing distribution 24 | break; 25 | } else if (grade<-1) { 26 | // do nothing if less than -1 27 | } else if (grade < 30) { 28 | this.gradeArray[0] += 1; 29 | } else if (grade < 35 && grade >= 30) { 30 | this.gradeArray[1] += 1; 31 | } else if (grade < 40 && grade >= 35) { 32 | this.gradeArray[2] += 1; 33 | } else if (grade < 45 && grade >= 40) { 34 | this.gradeArray[3] += 1; 35 | } else if (grade < 50 && grade >= 45) { 36 | this.gradeArray[4] += 1; 37 | } else if (grade <= 60 && grade >= 50) { 38 | this.gradeArray[5] += 1; 39 | } 40 | } 41 | } 42 | 43 | public void printGrades() { 44 | System.out.println("Grade distribution: "); 45 | System.out.print("5: "); 46 | this.printStars(5); 47 | System.out.print("4: "); 48 | this.printStars(4); 49 | System.out.print("3: "); 50 | this.printStars(3); 51 | System.out.print("2: "); 52 | this.printStars(2); 53 | System.out.print("1: "); 54 | this.printStars(1); 55 | System.out.print("0: "); 56 | this.printStars(0); 57 | System.out.print("Acceptance percentage: "); 58 | System.out.print(String.format( "%.1f", this.acceptance())); 59 | // ((can put this after)) ("Acceptance Percentage: " + String.format( "%.1f", this.acceptance()) 60 | } 61 | 62 | public void printStars(int gradeLevel) { 63 | int numberOfStars = this.gradeArray[gradeLevel]; // numberOfStars equals number in gradeArray at index gradeLevel 64 | 65 | for (int count = 0; count < numberOfStars; count++) { // while count less than gradeLevel, print a star 66 | System.out.print("*"); // print star 67 | } 68 | System.out.println(""); // go to next line after printing stars 69 | } 70 | 71 | public double acceptance() { 72 | int accept = this.gradeArray[1]+this.gradeArray[2]+this.gradeArray[3]+this.gradeArray[4]+this.gradeArray[5]; 73 | int fail = this.gradeArray[0]; 74 | 75 | /* 76 | for (int count = 1; count <= 5; count++) { 77 | int thisNum = this.gradeArray[count]; 78 | accept+=thisNum; 79 | 80 | } */ 81 | 82 | int total = accept + fail; 83 | 84 | if (accept==0) { 85 | return 0.0; 86 | } else if (fail==0) { 87 | return 100.0; 88 | } else { 89 | double acceptance = (((double) accept) / ((double) total)) *100.0; 90 | return acceptance; //With Input: 44, 12, 58, 29, 60, -1 -- THIS IS NOT WORKING!!! It rounds wrong somehow 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /Week 6 Exercise 102 Grade Distribution/Main: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner input = new Scanner(System.in); 6 | Grades classGrades = new Grades(input); 7 | 8 | System.out.println("Type exam scores, -1 completes:"); 9 | 10 | classGrades.getGrades(input); 11 | classGrades.printGrades(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Week 6 Exercise 103 Birdwatcher/Bird: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Bird { // *** created this at end of day, change class BirdWatch to incorporate Bird objects *** 4 | private String commonName; 5 | private String latinName; 6 | private int observed; 7 | 8 | public Bird(String commonName, String latinName) { 9 | this.commonName = commonName; 10 | this.latinName = latinName; 11 | this.observed = 0; 12 | } 13 | 14 | public void addObserved() { // method addObserved adds 1 to # of observations 15 | this.observed += 1; 16 | } 17 | 18 | public int returnObserved() { 19 | return this.observed; 20 | } 21 | 22 | public String returnName() { 23 | return this.commonName; 24 | } 25 | 26 | public String returnLatin() { 27 | return this.latinName; 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Week 6 Exercise 103 Birdwatcher/BirdWatch: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BirdWatch { // finish implementing Bird to this class. 4 | private ArrayList birds; 5 | 6 | public BirdWatch(Scanner input) { 7 | birds = new ArrayList(); 8 | } 9 | 10 | public void Add(Scanner input) { 11 | 12 | 13 | System.out.print("Name: "); 14 | String commonName = input.nextLine(); 15 | // code no longer needed: this.birds().get(Names().add(commonName); 16 | 17 | System.out.print("Latin Name: "); 18 | String latinName = input.nextLine(); 19 | // code no longer needed: this.latinNames().add(latinName); 20 | 21 | birds.add(new Bird(commonName, latinName)); // creates new Bird object and att it to ArrayList birds of Bird objects. 22 | } 23 | 24 | public void Observation(Scanner input) { 25 | System.out.print("What was observed:? "); 26 | String name = input.nextLine(); 27 | int searchNumber = this.search(name); // this.search(name) returns a number, either the index location of name, or -1 if name is not within this.birdNames 28 | 29 | if (searchNumber >= 0) { 30 | this.birds.get(searchNumber).addObserved(); // addObserved() method of Bird object, at index location searchNumber, run from ArrayList birds 31 | } else { 32 | System.out.println("This is not a bird!"); 33 | } 34 | } 35 | 36 | 37 | public int search(String name) { // returns the index location of name, in this.birds ArrayList. If name is not there, returns -1 38 | 39 | if (this.birds.size()==0) { 40 | return -1; 41 | } else { 42 | for (int count = 0; count <= this.birds.size(); count++) { 43 | 44 | String thisBird = this.birds.get(count).returnName(); // birdName is now the same as the commonName of the Bird object at index "count" 45 | 46 | if (this.wordMatch(name, thisBird)) { // What this does: if passed String parameter name matches thisBird String, an object instance of Bird object in birds ArrayList at index "count", return integer number of current index 47 | return count; 48 | } else { 49 | System.out.println("This is not a bird!"); 50 | } 51 | } 52 | } 53 | return -1; 54 | 55 | } 56 | 57 | public static boolean wordMatch(String word, String searched) { 58 | if (word == null || searched == null || word.isEmpty() || searched.isEmpty()) { 59 | return false; 60 | } 61 | 62 | word = word.trim(); 63 | word = word.toUpperCase(); 64 | searched = searched.trim(); 65 | searched = searched.toUpperCase(); 66 | 67 | if (word.contains(searched)) { 68 | return true; 69 | } else { 70 | return false; 71 | } 72 | } 73 | 74 | public void Show(Scanner input) { // last thing changed on this exercse... 75 | System.out.println("What?"); 76 | String whichBird = input.nextLine(); 77 | 78 | if (this.search(whichBird) >= 0) { // make sure the bird really exists. 79 | int atIndex = this.search(whichBird); // get index number of this bird name 80 | this.printBird(atIndex); // and print it 81 | } else { 82 | System.out.println("This is not a bird!"); 83 | } 84 | 85 | } 86 | 87 | public void Statistics() { 88 | for (int count = 0; count < birds.size(); count++) { // for each Bird object in ArrayList birds().... 89 | this.printBird(count); // printBird at index "count" 90 | System.out.println(""); 91 | } 92 | } 93 | 94 | public void printBird(int count) { 95 | System.out.print(this.birds.get(count).returnName()); // print commonName 96 | System.out.print(" (" + this.birds.get(count).returnLatin() + "): "); // print latinName 97 | System.out.print(this.birds.get(count).returnObserved() + " observations"); // print # of observations 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Week 6 Exercise 103 Birdwatcher/Main: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner input = new Scanner(System.in); 7 | BirdWatch deck = new BirdWatch(input); 8 | 9 | while (true) { 10 | System.out.print("? "); 11 | String command = input.nextLine(); 12 | 13 | if (command.equals("Quit")) { 14 | break; 15 | } else { 16 | UserChoice(input, command, deck); 17 | } 18 | } 19 | } 20 | 21 | public static void UserChoice(Scanner input, String command, BirdWatch deck) { 22 | 23 | if (command.equals("Add")) { 24 | deck.Add(input); 25 | } else if (command.equals("Observation")) { 26 | deck.Observation(input); 27 | } else if (command.equals("Show")) { 28 | deck.Show(input); 29 | } else if (command.equals("Statistics")) { 30 | deck.Statistics(); 31 | } else { 32 | System.out.println("Your input did not match a command. Try again!"); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Week 6 Exercise 104 Sorting Test/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | // write testcode here 4 | } 5 | 6 | public static int smallest(int[] array) { //104.1 7 | int smallest = array[0]; 8 | 9 | for (int count = 0; count < array.length; count++) { 10 | if (smallest > array[count]) { 11 | smallest = array[count]; 12 | } 13 | } 14 | 15 | return smallest; 16 | } 17 | 18 | public static int indexOfTheSmallest(int[] array) { //104.2 ((I changed this to incorporate an already-known index number as the starting point)) 19 | int index = indexOfTheSmallest(array, 0); 20 | return index; 21 | } 22 | 23 | public static int indexOfTheSmallest(int[] array, int startingIndex) { 24 | int smallest = array[startingIndex]; 25 | int index = startingIndex; 26 | 27 | for (int count = startingIndex; count < array.length; count++) { 28 | if (smallest > array[count]) { 29 | smallest = array[count]; 30 | index = count; 31 | } 32 | } 33 | return index; 34 | } 35 | 36 | public static int indexOfTheSmallestStartingFrom(int array[], int index) { //104.3 37 | int smallest = array[index]; 38 | int smallIndex = index; 39 | 40 | for (int count = index; count < array.length; count++) { 41 | if (smallest > array[count]) { 42 | smallest = array[count]; 43 | smallIndex = count; 44 | } 45 | } 46 | 47 | return smallIndex; 48 | } 49 | 50 | public static void swap(int[] array, int index1, int index2) { //104.4 51 | int firstNum = array[index1]; 52 | int secondNum = array[index2]; 53 | 54 | array[index1] = secondNum; 55 | array[index2] = firstNum; 56 | } 57 | 58 | public static void printArray(int[] array) { 59 | System.out.print("["); 60 | 61 | for (int count = 0; count < array.length; count++) { 62 | System.out.print("" +array[count]); 63 | 64 | if (count == array.length-1) { 65 | System.out.print("]"); 66 | } else { 67 | System.out.print(", "); 68 | } 69 | } 70 | } 71 | 72 | public static void sort(int[] array) { //104.5 73 | printArray(array); 74 | 75 | for (int count = 0; count < array.length; count++) { 76 | int index = indexOfTheSmallest(array, count); 77 | swap(array, count, index); 78 | 79 | printArray(array); 80 | } 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /Week 6 Exercise 105 Guessing Game/Guessing Game: -------------------------------------------------------------------------------- 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 boolean isGreaterThan(int value) { // 105.1 13 | System.out.println("Is your number greater than " + value + "? (y/n)"); 14 | String answer = reader.nextLine(); 15 | 16 | if (answer.equals("y")) { 17 | return true; 18 | } else { 19 | return false; 20 | } 21 | 22 | } 23 | 24 | public int average(int firstNumber, int secondNumber) { // 105.2 25 | return (firstNumber+secondNumber) / 2; 26 | } 27 | 28 | public void play(int lowerLimit, int upperLimit) { // 105.3 29 | int average; 30 | boolean greater; 31 | 32 | instructions(upperLimit,lowerLimit); 33 | 34 | while (true) { 35 | if (lowerLimit==upperLimit) { 36 | System.out.println("The number you're thinking of is " + upperLimit + "."); 37 | break; 38 | } 39 | 40 | average = this.average(lowerLimit, upperLimit); // get average of lower and upper 41 | greater = this.isGreaterThan(average); // is searchedFor larger than average? 42 | 43 | if (greater) { 44 | lowerLimit = average+1; 45 | } else { 46 | upperLimit = average; 47 | } 48 | } 49 | } 50 | 51 | // implement here the methods isGreaterThan and average 52 | 53 | public void instructions(int lowerLimit, int upperLimit) { 54 | int maxQuestions = howManyTimesHalvable(upperLimit - lowerLimit); 55 | 56 | System.out.println("Think of a number between " + lowerLimit + "..." + upperLimit + "."); 57 | 58 | System.out.println("I promise you that I can guess the number you are thinking with " + maxQuestions + " questions."); 59 | System.out.println(""); 60 | System.out.println("Next I'll present you a series of questions. Answer them honestly."); 61 | System.out.println(""); 62 | } 63 | 64 | // a helper method: 65 | public static int howManyTimesHalvable(int number) { 66 | // we create a base two logarithm of the given value 67 | 68 | // Below we swap the base number to base two logarithms! 69 | return (int) (Math.log(number) / Math.log(2)) + 1; 70 | } 71 | } 72 | 73 | /* 74 | 75 | 76 | 77 | 78 | 79 | */ 80 | -------------------------------------------------------------------------------- /Week 6 Exercise 105 Guessing Game/Main: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | // test your program here 4 | GuessingGame game = new GuessingGame(); 5 | game.play(1,10); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Week 6 Exercise 106 Binary Search/Binary Search: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | // Test your program here 8 | int[] array = {4, 5, 6, 7, 8, 9}; 9 | Scanner reader = new Scanner(System.in); 10 | 11 | System.out.print("Numbers in the array " + Arrays.toString(array)); 12 | System.out.println(); 13 | 14 | System.out.print("Enter searched number: "); 15 | String searchedNumber = reader.nextLine(); 16 | System.out.println(); 17 | 18 | boolean isThere = BinarySearch.search(array, Integer.parseInt(searchedNumber)); 19 | 20 | System.out.println(isThere); 21 | 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Week 6 Exercise 106 Binary Search/Main: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | // Test your program here 8 | int[] array = {4, 5, 6, 7, 8, 9}; 9 | Scanner reader = new Scanner(System.in); 10 | 11 | System.out.print("Numbers in the array " + Arrays.toString(array)); 12 | System.out.println(); 13 | 14 | System.out.print("Enter searched number: "); 15 | String searchedNumber = reader.nextLine(); 16 | System.out.println(); 17 | 18 | boolean isThere = BinarySearch.search(array, Integer.parseInt(searchedNumber)); 19 | 20 | System.out.println(isThere); 21 | 22 | } 23 | } 24 | 25 | --------------------------------------------------------------------------------