Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
New Beginner and Advanced Practice and Solutions#22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
03c3ff72a48437bb68ef0a2a2eb8b9f12383c4a093f1858cbcfc050cdb58c32542423b85bda2ce92fb41f6977c7cca23c247b6725199fb8740b86621b3fbfd661001084ad074640cb858017009e4e6db99ac5b2d2b5e02d2d8da5d17cb81dacd3db80995938508db4444713c4941332448dc66bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,4 +3,5 @@ | ||
| # ignore files | ||
| c4t-java.iml | ||
| .DS_Store | ||
| .DS_Store | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 {} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| /** | ||
armeetj marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| * 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)); | ||
| } | ||
armeetj marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| /** | ||
| * 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); | ||
armeetj marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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... | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(""); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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! | ||
| */ | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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!"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.