Repository navigation
Expand file tree
/
Copy pathLearning.java
More file actions
286 lines (205 loc) · 6.04 KB
/
Copy pathLearning.java
File metadata and controls
286 lines (205 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
Variables
String name = "John";
System.out.println(name);
int myNum = 15;
System.out.println(myNum);
myNum = 20;
System.out.println(myNum + " <-- This is a number");
int x = 5, y = 10, z = 15;
x = y = z = 50;
System.out.println(x + y + z);
boolean Ahaan = false;
Naming conventions:
- Variables must have unique names
- Should be descriptive
- Can contain letters, digits, _, and $
- Should start with a lowercase letter
- Names are case sensitive - myVar and myvar are different
*/
/*
Strings
String name = "Arnav";
String methods:
- name.equals("Arnav") --> true
- name.equalsIgnoreCase(arnav) --> true
- name.length() --> 5
- name.charAt(2) --> n
- name.indexOf("n") --> 2
- name.isEmpty() --> false
- name.toUpperCase() --> ARNAV
- name.toLowerCase() --> arnav
- name.trim() <-- removes all spaces from string
- name.replace("a", "e") --> Ernev
Formatted Strings
System.out.printf("Happy Birthday dear %s!" name); <-- review this more
*/
/*
For loops
for(initialization of counter: int i = 0; Condition: i < 10; Step: i++) {
}
Step:
- Increment: i++ or i+=(a number)
- Decrement: i-- or i-=(a number)
You can also set i equal to a variable!
(Also, side note, i can be named anything)
For loops with strings instead of numbers
for(String string : strings) {
}
*/
/*
While Loops
Scanner scanner = new Scanner(System.in);
String name = "";
while(name.isEmpty()){
System.out.print("Enter your name: ");
name = scanner.nextLine();
}
System.out.println("Hello " + name);
scanner.close();
or
Scanner scanner = new Scanner(System.in);
String response = "";
while(!response.equals("Q")){
System.out.println("You are playing a game: ");
System.out.print("Press Q to quit: ");
name = scanner.next().toUpperCase();
}
System.out.println("You have quit the game.")
scanner.close();
Do While Loop (executes inner statement once and then checks)
do{
System.out.println("Your age can't be negative");
System.out.print("Enter your age: ");
age = scanner.nextInt();
}while(age < 0);
*/
/*
Logical Operators
&& <-- AND
|| <-- OR
! <-- NOT
*/
/*
Scanner
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Your name is: " + name);
scanner.close()
You can also use:
- scanner.nextInt(); <-- to get an integer
- scanner.next(): <-- to get the next text before a space (if there is one)
- scanner.nextDouble(); <-- to get decimal values
- scanner.nextBoolean(); <-- to get booleans
Accepting a number and then accepting a string
scanner.nextInt();
scanner.nextLine();
scanner.nextLine();
Essentially, the enter after the integer counts as the string in nextLine, so this can be fixed by simply adding another nextLine
check without assigning it to a variable
*/
/*
Methods (place outside of the public static void main)
static void happyBirthday() {
System.out.println("Happy Birthday to you!");
System.out.println("Happy Birthday dear you!");
System.out.println("You are x years old!");
System.out.println("Happy Birthday to you!\n");
}
Call the above method with: happyBirthday()
Methods are not familiar with variables in other methods!
To use variables in other methods, set up the method like below (with an argument):
static void happyBirthday(String name) {
System.out.println("Happy Birthday to you!");
System.out.println("Happy Birthday dear you!");
System.out.println("You are x years old!");
System.out.println("Happy Birthday to you!\n");
}
Then, to call this, you can do: happyBirthday(name) <-- with name as an assigned variable in the method you are calling happyBirthday from
You can also add multiple arguments with mutliple variable types!
static double square(double number) {
return number * number;
}
Above is a method that returns a value, which is why it is not a static void and instead is a static double (since double is the return type)
The above method can also easily be written in one line:
static double square(double number) {return number * number;}
*/
/*
ArrayLists
import java.util.ArrayList;
ArrayList<Integer> list = new ArrayList<>();
list.add(3);
list.add(1);
list.add(2);
System.out.println(list);
ArrayList<String> fruits = new Arraylist<>();
fruits.add("Apple");
fruits.add("Orange");
ArrayList methods
fruits.add();
fruits.remove(1); <-- removes value at index
fruits.set(1, "Pineapple"); <-- replaces value at index with given value
fruits.size(); <-- returns the amount of elements
import java.util.Collections
Collections.sort(fruits); <-- alphabetically sorts the ArrayList
for(String fruit : fruits) {
System.out.println(fruit);
}
The above for loop iterates through the Arraylist
*/
/*
Object Oriented Programming
public class Car {
String make = "Ford";
String model = "Mustang";
int year = 2025;
double price = 58000;
boolean isRunning = false;
void start(){
isRunning = true;
}
void stop(){
isRunning = false;
}
}
To create a car object, write:
Car car = new Car();
To get a specific part of the object:
car.model / car.year (etc.)
System.out.println(car.isRunning());
car.start();
System.out.println(car.isRunning());
Constructors
public class Student {
String name;
int age;
double gpa;
boolean isEnrolled;
Student(String name, int age, double gpa, boolean isEnrolled) {
this.name = name;
this.age = age;
this.gpa = gpa;
this.isEnrolled = isEnrolled;
}
}
*/
/*
Writing Files
FileWriter - small / medium sized files
BufferedWriter - large sized files
PrintWriter - structured data (reports / logs)
FileOutputStream - binary files (images / audio)
import java.io.FileWriter;
import java.io.IOException;
FileWriter writer = new FileWriter("test.txt");
try(FileWriter writer = new FileWriter("test.txt")) {
writer.write("I like pizza!");
System.out.println("File has been written");
} catch(IOException e) {
System.out.println("Could not write file");
} cath(FileNotFoundException e) {
System.out.println("Could not locate file location");
}
*/