diff --git a/.gitignore b/.gitignore index 1dbb98b..0ec6afc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ # ignore files c4t-java.iml -.DS_Store \ No newline at end of file +.DS_Store + diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java new file mode 100644 index 0000000..04fd4f5 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java @@ -0,0 +1,27 @@ +package com.codefortomorrow.advanced.chapter16.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Implement a simple LinkedList + * You will need to create a LinkedListNode class, which represents each item/node in the linked list + * Assume that the elements in the linked list are all of type int + * Required functionality... + * - head(): get head of list + * - tail(): get tail of list + * - add(): add to tail of list + * - push(): push to head of list + * - pop(): remove head of list + * - toString(): return a String representation of the list + */ + +public class LinkedList { + + public static void main(String[] args) { + // write your code here + + } +} + +class LinkedListNode {} diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java new file mode 100644 index 0000000..a73917e --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -0,0 +1,164 @@ +package com.codefortomorrow.advanced.chapter16.solutions; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Implement a simple LinkedList + * You will need to create a LinkedListNode class, which represents each item/node in the linked list + * Assume that the elements in the linked list are all of type int + * Required functionality... + * - head(): get head of list + * - tail(): get tail of list + * - add(): add to tail of list + * - push(): push to head of list + * - pop(): remove head of list + * - toString(): return a String representation of the list + */ + +public class LinkedList { + + private LinkedListNode head; + + /** + * default constructor + */ + public LinkedList() { + head = null; + } + + /** + * constructor with first value + * @param value first element in the linked list + */ + public LinkedList(int value) { + // create first node + head = new LinkedListNode(value, null); + } + + /** + * get head of Linked List + * @return first element of the linked list + */ + public LinkedListNode head() { + return this.head; + } + + /** + * traverse and get tail of linked list + * @return last element of the linked list + */ + public LinkedListNode tail() { + LinkedListNode current = head; + if (current == null) return null; + + while (current.next() != null) { + current = current.next(); + } + + return current; + } + + /** + * add new element (node) to linked list + * @param value element to add to the end of the linked list + */ + public void add(int value) { + LinkedListNode tail = tail(); + if (tail == null) { + head = new LinkedListNode(value, null); + } else { + tail.setNext(new LinkedListNode(value, null)); + } + } + + /** + * push (add element to head of linkedlist) + */ + public void push(int value) { + LinkedListNode newHead = new LinkedListNode(value, head); + head = newHead; + } + + /** + * pop (remove and return head of linkedlist) + * @return the node that was removed + */ + public LinkedListNode pop() { + LinkedListNode popped = head; + head = head.next(); + return popped; + } + + /** + * Returns a String version of the LinkedList + * @return a String version of the LinkedList + */ + @Override + public String toString() { + String list = "["; + LinkedListNode current = head; + if (current == null) return null; + do { + list += Integer.toString(current.value()) + ", "; + current = current.next(); + } while (current != null); + + // Remove trailing comma and space after last list element + list = list.substring(0, list.length() - 2); + return list + "]"; + } +} + +class Node { + + private int value; + + /** + * Constructs a list node with the given value + * @param value the value stored in this Node + */ + public Node(int value) { + this.value = value; + } + + /** + * Returns the value of this Node + * @return the value of this Node + */ + public int value() { + return this.value; + } +} + +class LinkedListNode extends Node { + + private LinkedListNode next; + + /** + * Constructs a LinkedListNode + * @param value the value stored in this node + * @param next the next node + */ + public LinkedListNode(int value, LinkedListNode next) { + super(value); + this.next = next; + } + + /** + * Returns the next node + * @return the next node + */ + public LinkedListNode next() { + return this.next; + } + + /** + * Sets the next node + * @param next the next node + */ + public void setNext(LinkedListNode next) { + this.next = next; + return; + } +} diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java new file mode 100644 index 0000000..cebb705 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java @@ -0,0 +1,38 @@ +package com.codefortomorrow.beginner.chapter1.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * You'll be writing your first ever comment today! + * What we'll go over: + * - single line comments + * - multi line comments + */ + + // a class is an "object" where we will place all our code inside + public class Comments { + // the main method (below) is the first thing that runs when your program is run + public static void main(String[] args) { + // this is a single line comment, I can write anything here + // single line comments aren't run by Java! + // they can be used to annotate your code! + + /** + * this is a multi + * line + * comment + * + * It can span across multiple lines! + */ + + // YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below... + + + + + + + + } + } \ No newline at end of file diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java new file mode 100644 index 0000000..832742a --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java @@ -0,0 +1,19 @@ +package com.codefortomorrow.beginner.chapter1.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Welcome to Java! + * This may be your first ever java program! + * We'll begin your journey with the infamous Hello World program! + */ + + // a class is an "object" where we will place all our code inside + public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run + public static void main(String[] args) { + // write code here (replace the "" with "Hello World!") + System.out.println(""); + } + } diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java new file mode 100644 index 0000000..446b385 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java @@ -0,0 +1,38 @@ +package com.codefortomorrow.beginner.chapter1.solutions; + +/** + * @author ArmeetJatyani + * March 2021 + * + * You'll be writing your first ever comment today! + * What we'll go over: + * - single line comments + * - multi line comments + */ + + // a class is an "object" where we will place all our code inside + public class Comments { + // the main method (below) is the first thing that runs when your program is run + public static void main(String[] args) { + // this is a single line comment, I can write anything here + // single line comments aren't run by Java! + // they can be used to annotate your code! + + /** + * this is a multi + * line + * comment + * + * It can span across multiple lines! + */ + + // YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below... + + // Hi my name is Armeet! + + /** + * I like teaching Java, and + * good luck on your journey! + */ + } + } \ No newline at end of file diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java new file mode 100644 index 0000000..a1152d2 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java @@ -0,0 +1,19 @@ +package com.codefortomorrow.beginner.chapter1.solutions; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Welcome to Java! + * This may be your first ever java program! + * We'll begin your journey with the infamous Hello World program! + */ + + // a class is an "object" where we will place all our code inside + public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run + public static void main(String[] args) { + // write code here (replace the "" with "Hello World!") + System.out.println("Hello World!"); + } + } diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java new file mode 100644 index 0000000..bb28674 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java @@ -0,0 +1,28 @@ +package com.codefortomorrow.beginner.chapter2.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Print out the number of apples and oranges! + */ + + public class ApplesOranges { + public static void main(String[] args) { + // write your code here + + // define an integer variable called numOranges with value 10 (line 15) + + + // define an integer variable called numApples with value 24 (line 18) + + + // print out number of oranges using variables, output: "I have 10 oranges." (line 21) + + + // print out number of apples using variables, output: "I have 24 apples." (line 24) + + + } + } + diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java new file mode 100644 index 0000000..e5b9678 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java @@ -0,0 +1,34 @@ +package com.codefortomorrow.beginner.chapter2.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Define different types of variables + */ + + public class VariableTypes { + public static void main(String[] args) { + // write your code here + + // define an integer variable on line 15 + + + // define a float variable on line 18 + + + // define a double variable on line 21 + + + // define a boolean variable on line 24 (Hint: true/false) + + + // define a character variable on line 27 + + + // define a string variable on line 30 + + + } + } + diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java new file mode 100644 index 0000000..b35c1e9 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java @@ -0,0 +1,28 @@ +package com.codefortomorrow.beginner.chapter2.solutions; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Print out the number of apples and oranges! + */ + + public class ApplesOranges { + public static void main(String[] args) { + // write your code here + + // define an integer variable called numOranges with value 10 (line 15) + int numOranges = 10; + + // define an integer variable called numApples with value 24 (line 18) + int numApples = 24; + + // print out number of oranges using variables, output: "I have 10 oranges." (line 21) + System.out.println("I have " + numOranges + " oranges."); + + // print out number of apples using variables, output: "I have 24 apples." (line 24) + System.out.println("I have " + numApples + " apples."); + + } + } + diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java new file mode 100644 index 0000000..0a3a64e --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -0,0 +1,34 @@ +package com.codefortomorrow.beginner.chapter2.solutions; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Define different types of variables + */ + + public class VariableTypes { + public static void main(String[] args) { + // write your code here + + // define an integer variable on line 15 + int age = 23; + + // define a float variable on line 18 + float decimal = 23.32544f; + + // define a double variable on line 21 + double decimal = 23.2352536d; + + // define a boolean variable on line 24 (Hint: true/false) + double dogsOut = true; // the dogs are out :) + + // define a character variable on line 27 + char letter = 'A'; + + // define a string variable on line 30 + char name = "Jeff"; + + } + } + diff --git a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java new file mode 100644 index 0000000..df2013f --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java @@ -0,0 +1,28 @@ +package com.codefortomorrow.beginner.chapter4.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Manage a car dealership and take inventory of all cars sold! + */ + + public class CarDealership { + public static void main(String[] args) { + // write your code here + + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) + + + + + // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) + + + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) + + + + } + } + \ No newline at end of file diff --git a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java new file mode 100644 index 0000000..10fdd82 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java @@ -0,0 +1,28 @@ +package com.codefortomorrow.beginner.chapter4.solutions; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Manage a car dealership and take inventory of all cars sold! + */ + + public class CarDealership { + public static void main(String[] args) { + // write your code here + + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) + final int car1 = 30000; + final int car2 = 24650; + final int car3 = 253630; + + // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) + int revenue = car1 + car2 + car3; + + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) + System.out.println("I sold 3 cars for $" + revenue + "."); + + + } + } + \ No newline at end of file