-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorld.java
More file actions
71 lines (57 loc) · 1.84 KB
/
HelloWorld.java
File metadata and controls
71 lines (57 loc) · 1.84 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
/*
This example will use Hello World to explore the structure of
a Java program including classes, main function, constructors,
member data, and functions
*/
// This enables user input:
import java.util.Scanner;
// Creating the class:
public class HelloWorld {
// Member data
private String myName;
/* Constructor */
public HelloWorld(String myName) {
this.myName = myName;
}
public void sayHello() {
System.out.println("Hello World from sayHello");
}
public void sayHelloFromMe() {
// Use member data
System.out.println("Hello World from "+myName);
}
public void sayHelloFromYou(String yourName) {
// Use parameter
System.out.println("Hello World from "+yourName);
}
public void enterNum() {
// This is used for simple user input:
int num1, num2, sum;
Scanner scan = new Scanner(System.in);
System.out.print("Enter any number: ");
num1 = scan.nextInt();
System.out.println(" The number entered by user: "+num1);
System.out.print("Enter your second number: ");
num2 = scan.nextInt();
System.out.println(" The second number is: "+num2);
scan.close();
// Is the number positive or negative?
if(num1 > 0){
System.out.println(num1+" is a positive number");
} else if(num1 < 0) {
System.out.println(num1+" is a negative number");
} else {
System.out.println(num1+" is neither positive or negative");
}
sum = num1 + num2;
System.out.println("Sum of your two entries: "+sum);
}
public static void main(String[] args) {
System.out.println("Hello World from main");
HelloWorld hello = new HelloWorld("Bob"); // Create new hello object
hello.sayHello();
hello.sayHelloFromYou("Sue");
hello.sayHelloFromMe();
hello.enterNum();
}
}