├── README.md ├── assn01 └── GravityCalculator.java ├── assn02 └── FooCorporation.java ├── assn03 └── Marathon.java └── assn04 ├── Book.java └── Library.java /README.md: -------------------------------------------------------------------------------- 1 | # MIT6092-Introduction-to-Programming-in-Java_problem-sets 2 | Assignment solutions to the online course MIT 6.092 Introduction to Programming in Java (Jan 2010) 3 | 4 | [Course home](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-092-introduction-to-programming-in-java-january-iap-2010/index.htm) 5 | 6 | [Assignments](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-092-introduction-to-programming-in-java-january-iap-2010/assignments/) 7 | 8 | -------------------------------------------------------------------------------- /assn01/GravityCalculator.java: -------------------------------------------------------------------------------- 1 | 2 | class GravityCalculator { 3 | public static void main(String[] args) { 4 | double gravity = -9.81; 5 | double initialVelocity = 0.0; 6 | double fallingTime = 10.0; 7 | double initialPosition = 0.0; 8 | // double finalPosition = 0.0; 9 | double finalPosition = 0.5 * gravity * fallingTime * fallingTime + initialVelocity * fallingTime + initialPosition; 10 | System.out.println("The object's position after "+fallingTime+" seconds is "+finalPosition+" m."); 11 | } 12 | } -------------------------------------------------------------------------------- /assn02/FooCorporation.java: -------------------------------------------------------------------------------- 1 | 2 | public class FooCorporation { 3 | public static void calculatePay(int hour, double base) { 4 | if (base < 8.0) { 5 | System.err.println("Error: base pay is lower than $8.00/hour!"); 6 | return; 7 | } 8 | 9 | if (hour > 60) { 10 | System.err.println("Error: working hours more than 60!"); 11 | return; 12 | } else if (hour <= 40) { 13 | double pay = hour * base; 14 | System.out.println("Total pay: $"+pay); 15 | } else { 16 | double pay = 40*base + (hour-40)*base*1.5; 17 | System.out.println("Total pay: $"+pay); 18 | } 19 | 20 | } 21 | public static void main(String[] args) { 22 | calculatePay(35, 7.5); 23 | calculatePay(47, 8.2); 24 | calculatePay(73, 10); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /assn03/Marathon.java: -------------------------------------------------------------------------------- 1 | 2 | public class Marathon { 3 | 4 | public static int bestRunner(String[] runners, int[] times) { 5 | int fastest = Integer.MAX_VALUE; 6 | int fIndex = -1; 7 | 8 | for (int i = 0; i < times.length; i++) { 9 | if (times[i] < fastest) { 10 | fastest = times[i]; 11 | fIndex = i; 12 | } else { 13 | continue; 14 | } 15 | } 16 | return fIndex; 17 | } 18 | 19 | public static int getSecondMinIndex(String[] runners,int[] values) { 20 | int secondIdx = -1; 21 | int minIdx = bestRunner(runners, values); 22 | for (int i=0; i books = new ArrayList<>(); 9 | 10 | public Library(String address) { 11 | this.address = address; 12 | } 13 | 14 | public static void printOpeningHours() { 15 | System.out.println("Libraries are open daily from 9am to 5pm."); 16 | } 17 | 18 | public void addBook(Book book) { 19 | books.add(book); 20 | } 21 | 22 | public void printAddress() { 23 | System.out.println(this.address); 24 | } 25 | 26 | public void borrowBook(String bookTitle) { 27 | for (Book book : books) { 28 | if (book.getTitle() == bookTitle) { 29 | if (!book.isBorrowed()) { 30 | book.borrowed(); 31 | System.out.println("You successfully borrowed the "+bookTitle); 32 | return; 33 | } else { 34 | System.out.println("Sorry, this book is already borrowed."); 35 | return; 36 | } 37 | } else { 38 | continue; 39 | } 40 | } 41 | System.out.println("Sorry, this book is not in our catalog."); 42 | } 43 | 44 | public void printAvailableBooks() { 45 | if (this.books.size() > 0) { 46 | for (Book book : books) { 47 | if (!book.isBorrowed()) { 48 | System.out.println(book.getTitle()); 49 | } 50 | } 51 | } else { 52 | System.out.println("No book in catalog"); 53 | } 54 | } 55 | 56 | public void returnBook(String bookTitle) { 57 | for (Book book : books) { 58 | if (book.getTitle() == bookTitle) { 59 | book.returned(); 60 | System.out.println("You sucessfully returned "+bookTitle); 61 | } 62 | } 63 | } 64 | 65 | public static void main(String[] args) { 66 | // Create two libraries 67 | Library firstLibrary = new Library("10 Main St."); 68 | Library secondLibrary = new Library("228 Liberty St."); 69 | 70 | // Add four books to the first library 71 | firstLibrary.addBook(new Book("The Da Vinci Code")); 72 | firstLibrary.addBook(new Book("Le Petit Prince")); 73 | firstLibrary.addBook(new Book("A Tale of Two Cities")); 74 | firstLibrary.addBook(new Book("The Lord of the Rings")); 75 | 76 | // Print opening hours and the addresses 77 | System.out.println("Library hours:"); 78 | printOpeningHours(); 79 | System.out.println(); 80 | 81 | System.out.println("Library addresses:"); 82 | firstLibrary.printAddress(); 83 | secondLibrary.printAddress(); 84 | System.out.println(); 85 | 86 | // Try to borrow The Lords of the Rings from both libraries 87 | System.out.println("Borrowing The Lord of the Rings:"); 88 | firstLibrary.borrowBook("The Lord of the Rings"); 89 | firstLibrary.borrowBook("The Lord of the Rings"); 90 | secondLibrary.borrowBook("The Lord of the Rings"); 91 | System.out.println(); 92 | 93 | // Print the titles of all available books from both libraries 94 | System.out.println("Books available in the first library:"); 95 | firstLibrary.printAvailableBooks(); 96 | System.out.println(); 97 | System.out.println("Books available in the second library:"); 98 | secondLibrary.printAvailableBooks(); 99 | System.out.println(); 100 | 101 | // Return The Lords of the Rings to the first library 102 | System.out.println("Returning The Lord of the Rings:"); 103 | firstLibrary.returnBook("The Lord of the Rings"); 104 | System.out.println(); 105 | 106 | // Print the titles of available from the first library 107 | System.out.println("Books available in the first library:"); 108 | firstLibrary.printAvailableBooks(); 109 | } 110 | 111 | } 112 | --------------------------------------------------------------------------------