Skip to content

Commit 38d5303

Browse files
neha-peddintiactions-user
authored andcommitted
Bot: Prettified Java code!
1 parent 0fbc886 commit 38d5303

File tree

30 files changed

+128
-97
lines changed

30 files changed

+128
-97
lines changed

src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static void withdraw(BankAccount account, int amount) {
5252
}
5353

5454
class BankAccount {
55+
5556
private int balance;
5657

5758
public BankAccount(int balance) {

src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ interface Measurable {
4040
}
4141

4242
class Student implements Measurable {
43+
4344
private double GPA;
4445
private String name;
4546

src/com/codefortomorrow/advanced/chapter16/practice/Access.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.codefortomorrow.advanced.chapter16.practice;
2-
32
/*
43
Using the methods outlined in the “Benefits of LinkedLists” section,
54
fill both a LinkedList and an ArrayList with 10,000 elements.

src/com/codefortomorrow/advanced/chapter16/practice/Names.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.codefortomorrow.advanced.chapter16.practice;
2-
32
/*
43
Create a LinkedList of Strings, and ask the user to
54
input 10 names one by one into the list.

src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.codefortomorrow.advanced.chapter16.practice;
2-
32
/*
43
Adding an insert method to a SinglyLinkedList
54
(details in SinglyLinkedList DIY section).

src/com/codefortomorrow/advanced/chapter16/practice/Sort.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.codefortomorrow.advanced.chapter16.practice;
2-
32
/*
43
Write a method which takes in a LinkedList of Strings
54
and rearranges the elements (without creating a new list)

src/com/codefortomorrow/advanced/chapter16/solutions/Access.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.codefortomorrow.advanced.chapter16.solutions;
2+
23
import java.util.*;
34

45
/*
@@ -19,24 +20,21 @@ public static void main(String[] args) {
1920

2021
// Define lists with 10,000 elements.
2122
LinkedList<Integer> list1 = new LinkedList<Integer>();
22-
for(int i = 0; i < 10000; i++)
23-
list1.add(1);
23+
for (int i = 0; i < 10000; i++) list1.add(1);
2424

2525
ArrayList<Integer> list2 = new ArrayList<Integer>();
26-
for(int i = 0; i < 10000; i++)
27-
list2.add(1);
26+
for (int i = 0; i < 10000; i++) list2.add(1);
2827

2928
// Get time for each operation and output the difference.
3029
a = System.nanoTime();
3130
int c = list1.get(9999);
3231
b = System.nanoTime();
33-
System.out.println("\nLinked: " + (b-a) + " ns");
32+
System.out.println("\nLinked: " + (b - a) + " ns");
3433

3534
a = System.nanoTime();
3635
int d = list2.get(9999);
3736
b = System.nanoTime();
38-
System.out.println("ArrayList: " + (b-a) + " ns");
39-
37+
System.out.println("ArrayList: " + (b - a) + " ns");
4038
// Note that the ArrayList takes less time.
4139
}
4240
}

src/com/codefortomorrow/advanced/chapter16/solutions/Names.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.codefortomorrow.advanced.chapter16.solutions;
2+
23
import java.util.*;
34

45
/*
@@ -19,18 +20,19 @@
1920
*/
2021

2122
public class Names {
23+
2224
public static void main(String[] args) {
2325
LinkedList<String> list = new LinkedList<String>();
2426
Scanner sc = new Scanner(System.in);
25-
for(int i = 0; i < 10; i++) {
26-
System.out.print("Enter name " + (i+1) + ": ");
27-
list.add(sc.nextLine());
27+
for (int i = 0; i < 10; i++) {
28+
System.out.print("Enter name " + (i + 1) + ": ");
29+
list.add(sc.nextLine());
2830
}
2931
System.out.println(list);
3032

31-
for(int i = 0; i < list.size(); i++)
32-
if(list.get(i).length() <= 5)
33-
list.remove(i--);
33+
for (int i = 0; i < list.size(); i++) if (
34+
list.get(i).length() <= 5
35+
) list.remove(i--);
3436
System.out.println(list);
3537

3638
list.add(0, "Apple");
Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.codefortomorrow.advanced.chapter16.solutions;
2+
23
import java.util.*;
34

45
/*
@@ -36,77 +37,75 @@ Make sure you have a toString() method defined!
3637
// definition.
3738

3839
public class SinglyLinkedListInsert {
39-
Node head;
40-
41-
// add() method from SinglyLinkedList DIY section, adds to end of list
42-
public void add(int data) {
43-
// Create a new node with given data
44-
Node new_node = new Node(data);
45-
46-
// If the Linked List is empty,
47-
// then make the new node as head
48-
if (head == null) {
49-
head = new_node;
50-
}
51-
else {
52-
// Else traverse till the last node
53-
// and insert the new_node there
54-
Node last = head;
55-
while (last.next != null) {
56-
last = last.next;
57-
}
58-
59-
// Insert the new_node at last node
60-
last.next = new_node;
61-
}
62-
}
63-
64-
public void insert(int data, int pos) {
65-
// Verify valid pos number
66-
if (pos < 0)
67-
System.out.print("Invalid position");
68-
69-
// Create new node and keep track of current node
70-
Node new_node = new Node(data);
71-
Node current = head;
72-
73-
// If pos is 0, set new node as head
74-
if (pos == 0) {
75-
new_node.next = head;
76-
head = new_node;
77-
} else {
78-
while (pos-- >= 0) {
79-
if (pos == 0) {
80-
// Add new node after current position
81-
new_node.next = current.next;
82-
current.next = new_node;
83-
break;
40+
41+
Node head;
42+
43+
// add() method from SinglyLinkedList DIY section, adds to end of list
44+
public void add(int data) {
45+
// Create a new node with given data
46+
Node new_node = new Node(data);
47+
48+
// If the Linked List is empty,
49+
// then make the new node as head
50+
if (head == null) {
51+
head = new_node;
52+
} else {
53+
// Else traverse till the last node
54+
// and insert the new_node there
55+
Node last = head;
56+
while (last.next != null) {
57+
last = last.next;
58+
}
59+
60+
// Insert the new_node at last node
61+
last.next = new_node;
62+
}
63+
}
64+
65+
public void insert(int data, int pos) {
66+
// Verify valid pos number
67+
if (pos < 0) System.out.print("Invalid position");
68+
69+
// Create new node and keep track of current node
70+
Node new_node = new Node(data);
71+
Node current = head;
72+
73+
// If pos is 0, set new node as head
74+
if (pos == 0) {
75+
new_node.next = head;
76+
head = new_node;
77+
} else {
78+
while (pos-- >= 0) {
79+
if (pos == 0) {
80+
// Add new node after current position
81+
new_node.next = current.next;
82+
current.next = new_node;
83+
break;
84+
}
85+
// Check if pos is out of bounds (reaches end)
86+
if (current.next == null) {
87+
System.out.println("Out of bounds");
88+
break;
89+
}
90+
current = current.next;
91+
}
8492
}
85-
// Check if pos is out of bounds (reaches end)
86-
if(current.next == null) {
87-
System.out.println("Out of bounds");
88-
break;
93+
}
94+
95+
@Override
96+
public String toString() {
97+
// For empty lists
98+
if (head == null) return "{}";
99+
100+
String str = "{";
101+
102+
// Traverse the list until the last node
103+
Node current = head;
104+
while (current.next != null) {
105+
str += "" + current.data + ", ";
106+
current = current.next;
89107
}
90-
current = current.next;
91-
}
92-
}
93-
}
94-
95-
@Override
96-
public String toString() {
97-
// For empty lists
98-
if(head == null)
99-
return "{}";
100-
101-
String str = "{";
102-
103-
// Traverse the list until the last node
104-
Node current = head;
105-
while(current.next != null) {
106-
str += "" + current.data + ", ";
107-
current = current.next;
108+
str += current.data + "}";
109+
return str;
108110
}
109-
str += current.data + "}";
110-
return str;
111-
}
112111
}

src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.codefortomorrow.advanced.chapter16.solutions;
2+
23
import java.util.*;
34

45
/*
@@ -9,29 +10,38 @@ and rearranges the elements (without creating a new list)
910
*/
1011

1112
public class Sort {
13+
1214
public static void main(String[] args) {
1315
LinkedList<String> list = new LinkedList<String>();
14-
list.addAll(Arrays.asList(
15-
"Jeannette", "Sophia", "Computer", "Boots",
16-
"Lava", "Lasagna", "Dirty", "Cleanie"
17-
));
16+
list.addAll(
17+
Arrays.asList(
18+
"Jeannette",
19+
"Sophia",
20+
"Computer",
21+
"Boots",
22+
"Lava",
23+
"Lasagna",
24+
"Dirty",
25+
"Cleanie"
26+
)
27+
);
1828
sort(list);
1929
System.out.println(list);
2030
}
2131

2232
public static void sort(LinkedList<String> list) {
23-
if(list.size() < 2) return;
24-
for(int i = 1; i < list.size(); i++) {
33+
if (list.size() < 2) return;
34+
for (int i = 1; i < list.size(); i++) {
2535
String current = list.get(i);
2636
// Check if current word is smaller than previous word
27-
if(current.length() < list.get(i-1).length()) {
37+
if (current.length() < list.get(i - 1).length()) {
2838
// Add current word before the node with
2939
// same or greater word size
30-
for(int j = 0; j < i; j++) {
31-
if(list.get(j).length() >= current.length()) {
40+
for (int j = 0; j < i; j++) {
41+
if (list.get(j).length() >= current.length()) {
3242
list.add(j, current);
3343
// Remove duplicate current
34-
list.remove(i+1);
44+
list.remove(i + 1);
3545
break;
3646
}
3747
}

0 commit comments

Comments
 (0)