├── README.md ├── src └── com │ ├── cfs2160 │ ├── greeters │ │ └── HelloWorld.java │ ├── lectures │ │ ├── week08 │ │ │ ├── EmployeeDemo.java │ │ │ └── Employee.java │ │ ├── week10 │ │ │ └── TempReader.java │ │ ├── week09 │ │ │ ├── Contact.java │ │ │ ├── FileList.java │ │ │ └── PhoneBook.java │ │ └── week07 │ │ │ └── Employee.java │ ├── week08 │ │ └── BankAccount.java │ └── week07 │ │ └── Employee.java │ └── poppleton │ ├── crackers │ └── PossiblyUsefulCode.java │ ├── dogs │ ├── DogShowScore.java │ └── Dog.java │ └── christmas │ └── club │ └── Member.java ├── .gitignore └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # cfs2160-2019-java-public 2 | 3 | ## Java code for CFS2160 Programming Stream 2019/20. 4 | 5 | This code is provided "as is" with no guarantees. Please email if you find 6 | any errors. 7 | 8 | It will be updated after the lecture. 9 | -------------------------------------------------------------------------------- /src/com/cfs2160/greeters/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package com.cfs2160.greeters; 2 | 3 | public class HelloWorld { 4 | 5 | public HelloWorld () { 6 | this.sayHelloWorld (); 7 | } 8 | 9 | public void sayHelloWorld () { 10 | System.out.println ("Hello, World"); 11 | } 12 | 13 | public static void main (String[] args) { 14 | new HelloWorld (); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | /.idea/ 25 | /cfs2160-2019-java-public.iml 26 | -------------------------------------------------------------------------------- /src/com/cfs2160/lectures/week08/EmployeeDemo.java: -------------------------------------------------------------------------------- 1 | package com.cfs2160.lectures.week08; 2 | 3 | public class EmployeeDemo { 4 | 5 | public static void main (String[] args) { 6 | 7 | Employee bob = new Employee (1, "Bob", 10); 8 | 9 | System.out.println (bob); 10 | 11 | if (!bob.increaseRating ()) { 12 | System.out.println ("That didn't work."); 13 | } 14 | 15 | System.out.println (bob); 16 | 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/com/cfs2160/week08/BankAccount.java: -------------------------------------------------------------------------------- 1 | package com.cfs2160.week08; 2 | 3 | public class BankAccount { 4 | 5 | private String accountNumber; 6 | private String accountHolder; 7 | private double balance; 8 | private boolean hasOverdraft; 9 | 10 | // Constructor Here. 11 | // Code -> Generate -> Constructor 12 | 13 | // Getters and Setters Here. 14 | // Code -> Generate -> Getters and Setters 15 | 16 | // Deposit Method Here 17 | 18 | // Withdraw Method Here. 19 | 20 | // AddInterest Method Here. 21 | 22 | // toString Method Here. 23 | // Code -> Generate -> ... 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/com/poppleton/crackers/PossiblyUsefulCode.java: -------------------------------------------------------------------------------- 1 | package com.poppleton.crackers; 2 | 3 | import java.text.NumberFormat; 4 | import java.util.Locale; 5 | 6 | public class PossiblyUsefulCode { 7 | 8 | public static void main (String[] args) { 9 | 10 | double foo = 200.0; 11 | double bar = 1.999; 12 | 13 | Locale locale = new Locale("en", "GB"); 14 | NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale); 15 | 16 | System.out.println (currencyFormatter.format (foo)); 17 | System.out.println (currencyFormatter.format (bar)); 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/com/poppleton/dogs/DogShowScore.java: -------------------------------------------------------------------------------- 1 | package com.poppleton.dogs; 2 | 3 | public class DogShowScore { 4 | 5 | private String event; 6 | private int score; 7 | 8 | public DogShowScore (String event, int score) { 9 | this.event = event; 10 | this.score = score; 11 | } 12 | 13 | public String getEvent () { 14 | return event; 15 | } 16 | 17 | public void setEvent (String event) { 18 | this.event = event; 19 | } 20 | 21 | public int getScore () { 22 | return score; 23 | } 24 | 25 | public void setScore (int score) { 26 | this.score = score; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/com/cfs2160/lectures/week10/TempReader.java: -------------------------------------------------------------------------------- 1 | package com.cfs2160.lectures.week10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class TempReader { 6 | 7 | public static void main (String[] args) { 8 | 9 | Scanner in = new Scanner (System.in); 10 | 11 | System.out.print ("Enter the temperature: "); 12 | String s = in.nextLine (); 13 | 14 | try { 15 | if (s.endsWith ("C")) { 16 | int temp = Integer.parseInt (s.substring (0, s.length () - 1)); 17 | System.out.println ("temp = " + temp); 18 | } else { 19 | throw new NumberFormatException (); 20 | } 21 | } 22 | catch (NumberFormatException e) { 23 | System.out.println ("Wrong format"); 24 | } 25 | 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/com/cfs2160/lectures/week09/Contact.java: -------------------------------------------------------------------------------- 1 | package com.cfs2160.lectures.week09; 2 | 3 | public class Contact { 4 | 5 | private String name; 6 | private String number; 7 | 8 | public Contact () { 9 | } 10 | 11 | public Contact (String name, String number) { 12 | this.name = name; 13 | this.number = number; 14 | } 15 | 16 | public String getName () { 17 | return name; 18 | } 19 | 20 | public void setName (String name) { 21 | this.name = name; 22 | } 23 | 24 | public String getNumber () { 25 | return number; 26 | } 27 | 28 | public void setNumber (String number) { 29 | this.number = number; 30 | } 31 | 32 | @Override 33 | public String toString () { 34 | return "Name: " + this.name + ", Number: " + this.number; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /src/com/cfs2160/lectures/week08/Employee.java: -------------------------------------------------------------------------------- 1 | package com.cfs2160.lectures.week08; 2 | 3 | public class Employee { 4 | 5 | final static int MAX_RATING = 10; 6 | 7 | private int id; 8 | private String name; 9 | private int rating; 10 | 11 | public Employee (int id, String name) { 12 | this.id = id; 13 | this.name = name; 14 | this.rating = 0; 15 | } 16 | 17 | public Employee (int id, String name, int rating) { 18 | this.id = id; 19 | this.name = name; 20 | this.rating = rating; 21 | } 22 | 23 | public int getId () { 24 | return id; 25 | } 26 | 27 | public void setId (int id) { 28 | this.id = id; 29 | } 30 | 31 | public String getName () { 32 | return name; 33 | } 34 | 35 | public void setName (String name) { 36 | this.name = name; 37 | } 38 | 39 | public int getRating () { 40 | return rating; 41 | } 42 | 43 | public void setRating (int rating) { 44 | this.rating = rating; 45 | } 46 | 47 | public boolean increaseRating () { 48 | 49 | 50 | if (this.getRating () < MAX_RATING) { 51 | this.rating++; 52 | return true; 53 | } 54 | else { 55 | return false; 56 | } 57 | } 58 | 59 | @Override 60 | public String toString () { 61 | return "Employee{" + 62 | "id=" + id + 63 | ", name='" + name + '\'' + 64 | ", rating=" + rating + 65 | '}'; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/com/cfs2160/lectures/week07/Employee.java: -------------------------------------------------------------------------------- 1 | package com.cfs2160.lectures.week07; 2 | 3 | public class Employee { 4 | 5 | private int id; 6 | private String name; 7 | private int rating; 8 | 9 | public Employee (int id, String name) { 10 | this.id = id; 11 | this.name = name; 12 | this.rating = 0; 13 | } 14 | 15 | public Employee (int id, String name, int rating) { 16 | this.id = id; 17 | this.name = name; 18 | this.rating = rating; 19 | } 20 | 21 | public int getId () { 22 | return id; 23 | } 24 | 25 | public void setId (int id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName () { 30 | return name; 31 | } 32 | 33 | public void setName (String name) { 34 | this.name = name; 35 | } 36 | 37 | public int getRating () { 38 | return rating; 39 | } 40 | 41 | public void setRating (int rating) { 42 | this.rating = rating; 43 | } 44 | 45 | public void increaseRating () { 46 | this.rating ++; 47 | } 48 | 49 | @Override 50 | public String toString () { 51 | return "Employee{" + 52 | "id=" + id + 53 | ", name='" + name + '\'' + 54 | ", rating=" + rating + 55 | '}'; 56 | } 57 | 58 | public static void main (String[] args) { 59 | 60 | Employee bob = new Employee (1, "Bob", 5); 61 | 62 | System.out.println (bob); 63 | 64 | bob.increaseRating (); 65 | 66 | System.out.println (bob); 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/com/poppleton/christmas/club/Member.java: -------------------------------------------------------------------------------- 1 | package com.poppleton.christmas.club; 2 | 3 | public class Member { 4 | 5 | private String name; 6 | private int contribution; 7 | 8 | public Member (String name) { 9 | this.name = name; 10 | this.contribution = 0; 11 | } 12 | 13 | public Member (String name, int contribution) { 14 | this.name = name; 15 | this.contribution = contribution; 16 | } 17 | 18 | public String getName () { 19 | return name; 20 | } 21 | 22 | public void setName (String name) { 23 | this.name = name; 24 | } 25 | 26 | public int getContribution () { 27 | return contribution; 28 | } 29 | 30 | public void setContribution (int contribution) { 31 | this.contribution = contribution; 32 | } 33 | 34 | public boolean contribute (int amount) { 35 | if (amount > 0) { 36 | this.contribution += amount; 37 | return true; 38 | } 39 | else { 40 | return false; 41 | } 42 | } 43 | 44 | @Override 45 | public String toString () { 46 | return "Member: " + this.getName () + ". Contribution: " + this.getContribution () + '.'; 47 | } 48 | 49 | public static void main (String[] args) { 50 | 51 | Member theGrinch = new Member ("The Grinch"); 52 | Member cindyLouWho = new Member ("Cindy Lou Who"); 53 | 54 | theGrinch.contribute (0); 55 | theGrinch.contribute (-1000000); 56 | cindyLouWho.contribute (100); 57 | 58 | System.out.println (theGrinch); 59 | System.out.println (cindyLouWho); 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/com/cfs2160/lectures/week09/FileList.java: -------------------------------------------------------------------------------- 1 | package com.cfs2160.lectures.week09; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class FileList { 6 | 7 | private ArrayList files; 8 | 9 | public FileList () { 10 | this.files = new ArrayList (); 11 | } 12 | 13 | public void addFile (String newFile) { 14 | files.add (newFile); 15 | } 16 | 17 | public int getListSize () { 18 | return files.size (); 19 | } 20 | 21 | public void printFiles () { 22 | for (String s: files) { 23 | System.out.println (s); 24 | } 25 | } 26 | 27 | public void printMatchingFiles (String search) { 28 | for (String s: files) { 29 | if (s.contains (search)) { 30 | System.out.println (s); 31 | } 32 | } 33 | } 34 | 35 | public void printFirstMatchingFile (String search) { 36 | 37 | if (files.isEmpty ()) { 38 | System.out.println ("No files to search."); 39 | } 40 | else { 41 | 42 | boolean found = false; 43 | int index = 0; 44 | 45 | do { 46 | if (this.files.get (index).contains (search)) { 47 | System.out.println (files.get (index)); 48 | found = true; 49 | } else { 50 | index++; 51 | } 52 | } while (!found && index < files.size ()); 53 | } 54 | } 55 | 56 | public static void main (String[] args) { 57 | 58 | FileList fl = new FileList (); 59 | 60 | fl.addFile ("cheese.txt"); 61 | fl.addFile ("toast.doc"); 62 | fl.addFile ("sausage.doc"); 63 | fl.addFile ("cornflakes.jpg"); 64 | fl.addFile ("croissant.png"); 65 | 66 | System.out.println (fl.getListSize () + " items in the list."); 67 | fl.printFiles (); 68 | 69 | System.out.println ("doc files:"); 70 | fl.printMatchingFiles ("doc"); 71 | 72 | System.out.println ("First toast file:"); 73 | fl.printFirstMatchingFile ("toast"); 74 | 75 | System.out.println ("First bacon file:"); 76 | fl.printFirstMatchingFile ("bacon"); 77 | 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/com/cfs2160/lectures/week09/PhoneBook.java: -------------------------------------------------------------------------------- 1 | package com.cfs2160.lectures.week09; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class PhoneBook { 6 | 7 | private ArrayList contacts; 8 | 9 | public PhoneBook () { 10 | this.contacts = new ArrayList (); 11 | } 12 | 13 | public PhoneBook (ArrayList contacts) { 14 | this.contacts = contacts; 15 | } 16 | 17 | public void addContact (Contact newContact) { 18 | this.contacts.add (newContact); 19 | } 20 | 21 | public boolean printContact (String name) { 22 | 23 | for (Contact c: this.contacts) { 24 | if (c.getName ().equals (name)) { 25 | System.out.println (c); 26 | return true; 27 | } 28 | } 29 | return false; 30 | } 31 | 32 | public boolean searchContacts (String search) { 33 | 34 | boolean found = false; 35 | for (Contact c: this.contacts) { 36 | if (c.getName ().contains (search) || c.getNumber ().contains (search)) { 37 | System.out.println (c); 38 | found = true; 39 | } 40 | } 41 | return found; 42 | } 43 | 44 | public String toString () { 45 | String s = ""; 46 | 47 | s = "Current Contacts\n\n"; 48 | 49 | for (Contact c: this.contacts) { 50 | s += c.toString (); 51 | s += "\n"; 52 | } 53 | 54 | return s; 55 | } 56 | 57 | public static void main (String[] args) { 58 | 59 | Contact charlesBabbage = new Contact ("Charles Babbage", "0113 275 1235"); 60 | Contact adaLovelace = new Contact ("Ada Lovelace", "020 487 7362"); 61 | Contact alanTuring = new Contact ("Alan Turing", "0113 336 4112"); 62 | Contact jamesGosling = new Contact ("James Gosling", "+01 271 27261"); 63 | 64 | PhoneBook myContacts = new PhoneBook (); 65 | 66 | myContacts.addContact (charlesBabbage); 67 | myContacts.addContact (adaLovelace); 68 | myContacts.addContact (alanTuring); 69 | myContacts.addContact (jamesGosling); 70 | 71 | System.out.println (myContacts); 72 | 73 | System.out.println ("Looking for Ada's Number"); 74 | myContacts.printContact ("Ada Lovelace"); 75 | 76 | System.out.println (); 77 | System.out.println ("Looking for Leeds Phone Numbers."); 78 | myContacts.searchContacts ("0113"); 79 | 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/com/cfs2160/week07/Employee.java: -------------------------------------------------------------------------------- 1 | package com.cfs2160.week07; 2 | 3 | public class Employee { 4 | 5 | private int id; 6 | private String name; 7 | private String jobTitle; 8 | private String manager; 9 | private int salary; 10 | 11 | public Employee () { 12 | } 13 | 14 | public Employee (int id, String name) { 15 | this.id = id; 16 | this.name = name; 17 | } 18 | 19 | public Employee (int id, String name, String jobTitle, String manager, int salary) { 20 | this.id = id; 21 | this.name = name; 22 | this.jobTitle = jobTitle; 23 | this.manager = manager; 24 | this.salary = salary; 25 | } 26 | 27 | public int getId () { 28 | return id; 29 | } 30 | 31 | public void setId (int id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName () { 36 | return name; 37 | } 38 | 39 | public void setName (String name) { 40 | this.name = name; 41 | } 42 | 43 | public String getJobTitle () { 44 | return jobTitle; 45 | } 46 | 47 | public void setJobTitle (String jobTitle) { 48 | this.jobTitle = jobTitle; 49 | } 50 | 51 | public String getManager () { 52 | return manager; 53 | } 54 | 55 | public void setManager (String manager) { 56 | this.manager = manager; 57 | } 58 | 59 | public int getSalary () { 60 | return salary; 61 | } 62 | 63 | public void setSalary (int salary) { 64 | this.salary = salary; 65 | } 66 | 67 | public void giveSalaryRaise (int percentRaise) { 68 | this.salary += (this.getSalary () * (percentRaise / 100.0)); 69 | } 70 | 71 | public String parkingPermitStatus () { 72 | if ((this.jobTitle.startsWith ("Senior")) && (this.getSalary () > 22000)) { 73 | return "Employee is permitted a car parking space."; 74 | } 75 | else { 76 | return "Employee is not permitted a car parking space."; 77 | } 78 | } 79 | 80 | @Override 81 | public String toString () { 82 | final StringBuilder sb = new StringBuilder ("Employee{"); 83 | 84 | sb.append ("id=").append (id); 85 | sb.append (", name='").append (name).append ('\''); 86 | sb.append (", jobTitle='").append (jobTitle).append ('\''); 87 | sb.append (", manager='").append (manager).append ('\''); 88 | sb.append (", salary=").append (salary); 89 | sb.append ('}'); 90 | 91 | return sb.toString (); 92 | } 93 | 94 | public static void main (String[] args) { 95 | 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/com/poppleton/dogs/Dog.java: -------------------------------------------------------------------------------- 1 | package com.poppleton.dogs; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | public class Dog { 7 | 8 | private String name; 9 | private String breed; 10 | private String owner; 11 | 12 | private ArrayList showScores; 13 | 14 | 15 | public Dog (String name, String breed, String owner) { 16 | this.name = name; 17 | this.breed = breed; 18 | this.owner = owner; 19 | 20 | this.showScores = new ArrayList<> (); 21 | } 22 | 23 | public String getName () { 24 | return name; 25 | } 26 | 27 | public void setName (String name) { 28 | this.name = name; 29 | } 30 | 31 | public String getBreed () { 32 | return breed; 33 | } 34 | 35 | public void setBreed (String breed) { 36 | this.breed = breed; 37 | } 38 | 39 | public String getOwner () { 40 | return owner; 41 | } 42 | 43 | public void setOwner (String owner) { 44 | this.owner = owner; 45 | } 46 | 47 | public void addScore (DogShowScore newScore) { 48 | this.showScores.add (newScore); 49 | } 50 | 51 | public int getNumberOfEventsEntered () { 52 | return this.showScores.size (); 53 | } 54 | 55 | public double getShowScore () { 56 | 57 | final int scoreCount = this.showScores.size (); 58 | double score = 0; 59 | int[] scores = new int [scoreCount]; 60 | 61 | if (scoreCount >= 3) { 62 | for (int i = 0; i < scoreCount; i++) { 63 | scores[i] = this.showScores.get (i).getScore (); 64 | } 65 | 66 | Arrays.sort (scores); 67 | 68 | score = (scores[scoreCount - 1] + scores[scoreCount - 2] + scores[scoreCount - 3]) / 3.0; 69 | } 70 | 71 | return score; 72 | 73 | } 74 | 75 | public int getBestScore () { 76 | 77 | int bestScore = 0; 78 | 79 | for (DogShowScore score: this.showScores) { 80 | if (score.getScore () > bestScore) { 81 | bestScore = score.getScore (); 82 | } 83 | } 84 | 85 | return bestScore; 86 | } 87 | 88 | public String getBestEvent () { 89 | 90 | int bestScore = this.getBestScore (); 91 | 92 | if (bestScore == 0) { 93 | return ""; 94 | } 95 | else { 96 | for (DogShowScore score: this.showScores) { 97 | if (score.getScore () == bestScore) { 98 | return score.getEvent (); 99 | } 100 | } 101 | } 102 | 103 | return ""; 104 | 105 | } 106 | 107 | public String getOwnerDetailsAsString () { 108 | 109 | return this.getName () + " the dog is owned by " + this.getOwner () + "."; 110 | 111 | } 112 | 113 | } 114 | --------------------------------------------------------------------------------