-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleIoLecture.java
More file actions
229 lines (156 loc) · 6.62 KB
/
ConsoleIoLecture.java
File metadata and controls
229 lines (156 loc) · 6.62 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
import java.util.Scanner;
//public class ConsoleIoLecture {
//
// public static void main(String[] args) {
// ================================== print() and println()
// this:
// System.out.println("here");
// System.out.println("there");
// is equivalent to this:
// System.out.print("here\n");
// System.out.print("there\n");
// without the newline characters, print outputs this:
// System.out.print("here");
// System.out.print("there");
// to concatenate, just like JS:
// System.out.println("Hello" + " " + "World");
// ================================== printf() / .format()
// Print a formatted string using the following... printf(formatString, data)
// same output as print():
// System.out.printf("Hello");
// System.out.printf("World!");
// creating a string variable:
// String bigBall = "World";
// System.out.printf("Hello %s %s", "Titan", "Folks");
// System.out.printf("Hello %S", bigBall); // all caps
// multiple variables:
// int three = 3;
// String typeOfPetGroup = "cats";
// System.out.printf("I have %d %s.", three, typeOfPetGroup);
// currency:
// int currencyPennies = 1000;
// System.out.printf("I'll sell you swamp land for $%.2f an acre!", currencyPennies / 100.00);
// String myName = String.format("%s %s", "Justin", "Reich");
// System.out.println(myName);
// ================================== IMPORTS
// Scanner sc = new Scanner(System.in);
//
// System.out.println("enter something you like");
//
// String input = sc.nextLine();
//
// System.out.println("You've entered " + input);
// ================================== USER INPUT
// Scanner sc = new Scanner(System.in);
// System.out.println("Enter your first name!");
// String firstName = scanner.next();
// System.out.println("Enter your last name!");
// String lastName = scanner.next();
//// System.out.println("Your name is: " + firstName + " " + lastName);
// System.out.printf("Your name is: %s %s", firstName, lastName);
// .next() captures each input separated by a string:
//
// System.out.println(firstName);
// System.out.println(lastName);
// System.out.println(what);
// .nextInt() captures the first valid int value:
// System.out.print("Please enter your age: ");
// int age = sc.nextInt();
// System.out.println(age);
// .nextLine():
// System.out.print("Favorite quote: ");
// String quote = sc.nextLine();
// System.out.println(quote);
// Quirk of using next() then nextLine()...
// https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo
// https://coderanch.com/wiki/676482/Scanner-nextLine-gotcha
// Scanner sc = new Scanner(System.in);
// System.out.print("Please enter your favorite number: ");
// int num = sc.nextInt();
// System.out.println(num);
// sc.nextLine(); // needed to clear new line character from scanner
// System.out.print("Please enter your favorite words: ");
// String words = sc.nextLine();
// System.out.println(words);
// }
//
//}
import java.util.Scanner;
//public class ConsoleIoLecture {
//
// public static void main(String[] args) {
// ==================== Slide 3
// no >== ,or <==, or === because only value comparisons are available
// truthy and falsy values don't exist in Java
// int x = 2;
// int y = 3;
// if (x != y) {
// System.out.println("x is less than y");
// }
// ==================== Slide 5
// && will only check second value if first is true
// & will check both
// this can be used to check what would otherwise result in a syntax error
// same comparison process for || and |
// x will only increment in second condition if first condition is true using &&
// int x = 2;
// int y = 3;
//
// if (true || ++x == y) {
// System.out.println("Equal");
// } else {
// System.out.println("Not equal");
// }
// System.out.println(x);
// ==================== Slide 7
// DO NOT COMPARE STRINGS WITH == !!!!
// System.out.println("hello" == "world");
// Scanner sc = new Scanner(System.in);
// String cat1 = sc.nextLine();
// String cat2 = "cat";
// System.out.println(cat1);
// System.out.println(cat2);
// System.out.println(cat1.equalsIgnoreCase(cat2));
// do use equals(),
// boolean stringValuesAreEqual = "Test".equals("Tes");
// System.out.printf("The result is: %b", stringValuesAreEqual);
// or use equalsIgnoreCase()
//String hello = "hello";
//System.out.println(hello.equalsIgnoreCase("HeLlO"));
// does not equal
// String tech = "tech";
// System.out.println(!tech.equalsIgnoreCase("tool"));
// ==================== Slides 8 - 16
// basically the same as JS :)
// String caseSwitch = "Fred";
// switch (caseSwitch) {
// case "bob":
// System.out.println("Case 1");
// break;
// case "fred":
// System.out.println("Case 2");
// break;
// default :
// System.out.println("Default case");
// break;
// }
// }
//
//}
public class ConsoleIoLecture {
// TODO: create a method, sayName, that can take in a single name String input or two name String inputs and will return a greeting message to the user by either their first or first and last name.
public static String sayName(String name){
return "hi " + name ;
}
public static String sayName(String first, String last){
return "hi " + first + last;
}
public static void main(String[] args) {
System.out.println(sayName("Kenneth"));
System.out.println(sayName("Kenneth ", "Hayles"));
// TODO: overload the math exercises from the curriculum exercise to work with both integers and doubles
//public static int add(int num1, int num2) {
// return num1 + num2;
// }
}
}