Skip to content

Commit 1a9bd2f

Browse files
authored
Merge pull request #22 from ArmeetJatyani/master
New Beginner and Advanced Practice and Solutions
2 parents e2308f5 + 48dc66b commit 1a9bd2f

File tree

13 files changed

+487
-1
lines changed

13 files changed

+487
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44
# ignore files
55
c4t-java.iml
6-
.DS_Store
6+
.DS_Store
7+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.codefortomorrow.advanced.chapter16.practice;
2+
3+
/**
4+
* @author ArmeetJatyani
5+
* March 2021
6+
*
7+
* Implement a simple LinkedList
8+
* You will need to create a LinkedListNode class, which represents each item/node in the linked list
9+
* Assume that the elements in the linked list are all of type int
10+
* Required functionality...
11+
* - head(): get head of list
12+
* - tail(): get tail of list
13+
* - add(): add to tail of list
14+
* - push(): push to head of list
15+
* - pop(): remove head of list
16+
* - toString(): return a String representation of the list
17+
*/
18+
19+
public class LinkedList {
20+
21+
public static void main(String[] args) {
22+
// write your code here
23+
24+
}
25+
}
26+
27+
class LinkedListNode {}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package com.codefortomorrow.advanced.chapter16.solutions;
2+
3+
/**
4+
* @author ArmeetJatyani
5+
* March 2021
6+
*
7+
* Implement a simple LinkedList
8+
* You will need to create a LinkedListNode class, which represents each item/node in the linked list
9+
* Assume that the elements in the linked list are all of type int
10+
* Required functionality...
11+
* - head(): get head of list
12+
* - tail(): get tail of list
13+
* - add(): add to tail of list
14+
* - push(): push to head of list
15+
* - pop(): remove head of list
16+
* - toString(): return a String representation of the list
17+
*/
18+
19+
public class LinkedList {
20+
21+
private LinkedListNode head;
22+
23+
/**
24+
* default constructor
25+
*/
26+
public LinkedList() {
27+
head = null;
28+
}
29+
30+
/**
31+
* constructor with first value
32+
* @param value first element in the linked list
33+
*/
34+
public LinkedList(int value) {
35+
// create first node
36+
head = new LinkedListNode(value, null);
37+
}
38+
39+
/**
40+
* get head of Linked List
41+
* @return first element of the linked list
42+
*/
43+
public LinkedListNode head() {
44+
return this.head;
45+
}
46+
47+
/**
48+
* traverse and get tail of linked list
49+
* @return last element of the linked list
50+
*/
51+
public LinkedListNode tail() {
52+
LinkedListNode current = head;
53+
if (current == null) return null;
54+
55+
while (current.next() != null) {
56+
current = current.next();
57+
}
58+
59+
return current;
60+
}
61+
62+
/**
63+
* add new element (node) to linked list
64+
* @param value element to add to the end of the linked list
65+
*/
66+
public void add(int value) {
67+
LinkedListNode tail = tail();
68+
if (tail == null) {
69+
head = new LinkedListNode(value, null);
70+
} else {
71+
tail.setNext(new LinkedListNode(value, null));
72+
}
73+
}
74+
75+
/**
76+
* push (add element to head of linkedlist)
77+
*/
78+
public void push(int value) {
79+
LinkedListNode newHead = new LinkedListNode(value, head);
80+
head = newHead;
81+
}
82+
83+
/**
84+
* pop (remove and return head of linkedlist)
85+
* @return the node that was removed
86+
*/
87+
public LinkedListNode pop() {
88+
LinkedListNode popped = head;
89+
head = head.next();
90+
return popped;
91+
}
92+
93+
/**
94+
* Returns a String version of the LinkedList
95+
* @return a String version of the LinkedList
96+
*/
97+
@Override
98+
public String toString() {
99+
String list = "[";
100+
LinkedListNode current = head;
101+
if (current == null) return null;
102+
do {
103+
list += Integer.toString(current.value()) + ", ";
104+
current = current.next();
105+
} while (current != null);
106+
107+
// Remove trailing comma and space after last list element
108+
list = list.substring(0, list.length() - 2);
109+
return list + "]";
110+
}
111+
}
112+
113+
class Node {
114+
115+
private int value;
116+
117+
/**
118+
* Constructs a list node with the given value
119+
* @param value the value stored in this Node
120+
*/
121+
public Node(int value) {
122+
this.value = value;
123+
}
124+
125+
/**
126+
* Returns the value of this Node
127+
* @return the value of this Node
128+
*/
129+
public int value() {
130+
return this.value;
131+
}
132+
}
133+
134+
class LinkedListNode extends Node {
135+
136+
private LinkedListNode next;
137+
138+
/**
139+
* Constructs a LinkedListNode
140+
* @param value the value stored in this node
141+
* @param next the next node
142+
*/
143+
public LinkedListNode(int value, LinkedListNode next) {
144+
super(value);
145+
this.next = next;
146+
}
147+
148+
/**
149+
* Returns the next node
150+
* @return the next node
151+
*/
152+
public LinkedListNode next() {
153+
return this.next;
154+
}
155+
156+
/**
157+
* Sets the next node
158+
* @param next the next node
159+
*/
160+
public void setNext(LinkedListNode next) {
161+
this.next = next;
162+
return;
163+
}
164+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.codefortomorrow.beginner.chapter1.practice;
2+
3+
/**
4+
* @author ArmeetJatyani
5+
* March 2021
6+
*
7+
* You'll be writing your first ever comment today!
8+
* What we'll go over:
9+
* - single line comments
10+
* - multi line comments
11+
*/
12+
13+
// a class is an "object" where we will place all our code inside
14+
public class Comments {
15+
// the main method (below) is the first thing that runs when your program is run
16+
public static void main(String[] args) {
17+
// this is a single line comment, I can write anything here
18+
// single line comments aren't run by Java!
19+
// they can be used to annotate your code!
20+
21+
/**
22+
* this is a multi
23+
* line
24+
* comment
25+
*
26+
* It can span across multiple lines!
27+
*/
28+
29+
// YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below...
30+
31+
32+
33+
34+
35+
36+
37+
}
38+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codefortomorrow.beginner.chapter1.practice;
2+
3+
/**
4+
* @author ArmeetJatyani
5+
* March 2021
6+
*
7+
* Welcome to Java!
8+
* This may be your first ever java program!
9+
* We'll begin your journey with the infamous Hello World program!
10+
*/
11+
12+
// a class is an "object" where we will place all our code inside
13+
public class HelloWorld {
14+
// the main method (below) is the first thing that runs when your program is run
15+
public static void main(String[] args) {
16+
// write code here (replace the "" with "Hello World!")
17+
System.out.println("");
18+
}
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.codefortomorrow.beginner.chapter1.solutions;
2+
3+
/**
4+
* @author ArmeetJatyani
5+
* March 2021
6+
*
7+
* You'll be writing your first ever comment today!
8+
* What we'll go over:
9+
* - single line comments
10+
* - multi line comments
11+
*/
12+
13+
// a class is an "object" where we will place all our code inside
14+
public class Comments {
15+
// the main method (below) is the first thing that runs when your program is run
16+
public static void main(String[] args) {
17+
// this is a single line comment, I can write anything here
18+
// single line comments aren't run by Java!
19+
// they can be used to annotate your code!
20+
21+
/**
22+
* this is a multi
23+
* line
24+
* comment
25+
*
26+
* It can span across multiple lines!
27+
*/
28+
29+
// YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below...
30+
31+
// Hi my name is Armeet!
32+
33+
/**
34+
* I like teaching Java, and
35+
* good luck on your journey!
36+
*/
37+
}
38+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codefortomorrow.beginner.chapter1.solutions;
2+
3+
/**
4+
* @author ArmeetJatyani
5+
* March 2021
6+
*
7+
* Welcome to Java!
8+
* This may be your first ever java program!
9+
* We'll begin your journey with the infamous Hello World program!
10+
*/
11+
12+
// a class is an "object" where we will place all our code inside
13+
public class HelloWorld {
14+
// the main method (below) is the first thing that runs when your program is run
15+
public static void main(String[] args) {
16+
// write code here (replace the "" with "Hello World!")
17+
System.out.println("Hello World!");
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.codefortomorrow.beginner.chapter2.practice;
2+
3+
/**
4+
* @author ArmeetJatyani
5+
* March 2021
6+
*
7+
* Print out the number of apples and oranges!
8+
*/
9+
10+
public class ApplesOranges {
11+
public static void main(String[] args) {
12+
// write your code here
13+
14+
// define an integer variable called numOranges with value 10 (line 15)
15+
16+
17+
// define an integer variable called numApples with value 24 (line 18)
18+
19+
20+
// print out number of oranges using variables, output: "I have 10 oranges." (line 21)
21+
22+
23+
// print out number of apples using variables, output: "I have 24 apples." (line 24)
24+
25+
26+
}
27+
}
28+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codefortomorrow.beginner.chapter2.practice;
2+
3+
/**
4+
* @author ArmeetJatyani
5+
* March 2021
6+
*
7+
* Define different types of variables
8+
*/
9+
10+
public class VariableTypes {
11+
public static void main(String[] args) {
12+
// write your code here
13+
14+
// define an integer variable on line 15
15+
16+
17+
// define a float variable on line 18
18+
19+
20+
// define a double variable on line 21
21+
22+
23+
// define a boolean variable on line 24 (Hint: true/false)
24+
25+
26+
// define a character variable on line 27
27+
28+
29+
// define a string variable on line 30
30+
31+
32+
}
33+
}
34+

0 commit comments

Comments
 (0)