-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleIntro.java
More file actions
60 lines (59 loc) · 2.83 KB
/
ExampleIntro.java
File metadata and controls
60 lines (59 loc) · 2.83 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
public class ExampleIntro
{
String instancevariable = "instancevariable"; // instance variable as inside a class but outside a method, can be accessed anywhere in this class
public static String staticvariable = "staticvariable"; // static variable ("class variable"), only single copy exists, independent of object, can be accessed via dot notation
public ExampleIntro() // default no-argument constructor, always runs once when creating object (even if not written), construct object from the class
{
System.out.println("default constructor");
}
public void method1(String parameter1) // begin method definition, method with no return type that expects a string value to be passed to it
{
System.out.println("method1 " + parameter1); // method body, prints to console including the parameter passed to the method
}
public void method2(String parameter1, String parameter2) // method with no return type that expects two string values to be passed to it
{
System.out.println("method2 " + parameter1 + parameter2);
}
public void method3(String parameter1, String parameter2, String parameter3) // method with no return type that expects three string values to be passed to it
{
System.out.println("method3 " + parameter1 + parameter2 + parameter3);
}
public void method4() // method that doesn't expect any parameters to be passed to it
{
System.out.println("method4 " + instancevariable); // method body uses the instance variable
}
public void method5() // method that doesn't expect any parameters to be passed to it
{
System.out.println("method5 " + staticvariable); // method body uses the static variable
}
public String IfElseOddOrEven(int num) // function method as there's a return type (string), expects an integer passed into it
{
if (num % 2 == 0) // if this statement is true...
{
return "Even"; // return the function as this string
}
else // if the statement was false...
{
return "Odd"; // return the function as this string
}
}
public void ForLoop(int from, int to)
{
int loop; // method local integer variable to count the loops
// start the loop from the first (from) parameter passed
// run the code as long as this condition isn't met (current loop less then second parameter passed (to))
// increment the loop count by one each loop
for(loop = from; loop <= to; loop++)
System.out.println(loop); // print the value of the current loop variable
}
public void WhileLoop(int from, int to)
{
int loop; // method local integer variable to count the loops
loop = from; // set the loop count integer to the first (from) parameter passed
while(loop <= to) // keep looping as long as this condition is met (current loop less then second parameter passed (to))
{
System.out.println(loop); // print the value of the current loop variable
loop++; // increment the loop count by one each loop
}
}
}