forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson35.java
More file actions
149 lines (94 loc) · 4 KB
/
JavaLesson35.java
File metadata and controls
149 lines (94 loc) · 4 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
import java.lang.Math.*;
public class JavaLesson35{
// ERROR 1: Cannot make a static reference to the non-static method
// SOLVED: You can't call a non static method from a static method
// private void printSomething(){
private static void printSomething(){
// ERROR 2: Unresolved compilation problem
// SOLVED: Pay attention to Eclipse Errors
// Int BigNumber = 100000;
// ERROR 3: A string literal isn't properly closed
// SOLVED: Pay attention to Eclipse
//String something = "A string error
//is occurring";
System.out.println("Something");
} // If this is missing you get ERROR 2
// ERROR 4: Exception in thread "main" java.lang.NoSuchMethodError: main
// SOLVED: Make sure you type the main function correctly
// public static void main(String args){ // This is Wrong
public static void main(String[] args){
// ERROR 5: Can't be resolved to a variable
// SOLVED: Pay attention to Eclipse
// printsomething; // This is Wrong
printSomething();
// ERROR 6: Type mismatch Can't convert from int to String
// SOLVED: Convert the integer
int number = 12;
// String anotherNum = number; // This is Wrong
String anotherNum = Integer.toString(number);
// int number = Integer.parseInt(anotherNum); // Convert from string to int
// ERROR 7: Can't be resolved to a type
// SOLVED: import the dimension library
// Dimension dim = new Dimension();
// ERROR 8: Method is undefined
// SOLVED: Make sure methods are in the class
double pi = 3.14;
// long randLong = Lesson34.round(pi); // The wrong way
// ERROR 9: Can't invoke method
// SOLVED: Understand how methods work
long randLong = Math.round(pi); // The right way
// randLong = pi.round(); // Wrong way
// ERROR 11: The method is not applicable for the arguments
// SOLVED: Provide the right arguments in the right order
// getStuff(1.234, 5); // Wrong Way
getStuff(1,5.0); // Right Way
// ERROR 12: Syntax error on token ",; expected
// SOLVED: Understand how methods are called in Java vs. other languages
// double sumNum = addThem(LessonFive,1,2); // Wrong Way
double sumNum = JavaLessonFive.addThem(1,2);
// ERROR 13: Syntax error on token '=='
// SOLVED: = is different from ==
// int value == 1;
}
// ERROR 10: Can't be resolved to a type
// SOLVED: Always provide the type in methods
/* public static void getStuff(number1, number2){
} */
// ERROR 14: Return type for method is missing
// SOLVED: Provide a return type or void
public static void getStuff(int number1, double number2){
// ERROR 15: Syntax error on token ",[ expected
// SOLVED: Understand how arrays are defined in Java
// int[] intArray = new [10,10]int; // Wrong Way
int[][] intArray = new int[10][10];
// ERROR 16: The method is not visible
// SOLVED: You can't private methods which are declared in
// another class. private static void getFileInfo()
// Lesson33.getFileInfo(); // Wrong Way
// ERROR 17: Local variable may not have been initialized
// SOLVED: Always give variables default values
String howMany = "10";
// String howMany; // Wrong Way
System.out.println(howMany);
// ERROR 18: Cannot be Resolved
// SOLVED: Understand that arrays and strings use a
// different version of length
System.out.println(howMany.length());
// System.out.println(howMany.length); // Wrong Way
// System.out.println(intArray.length()); // Wrong Way
System.out.println(intArray.length);
// ERROR 19: Prefix Operator vs. Postfix Operator
int xInt = 1, yInt = 1;
xInt = yInt++; // Passes the original value of yInt before incrementing
System.out.println("xInt: " + xInt);
// ERROR 20: Not calling break at end of case
int day = 1;
switch (day){
case 1: System.out.println("Monday");
case 2: System.out.println("Tuesday");
case 3: System.out.println("Wednesday");
case 4: System.out.println("Thursday");
default: System.out.println("Friday");
}
}
}