-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount2.java
More file actions
61 lines (33 loc) · 1.21 KB
/
Account2.java
File metadata and controls
61 lines (33 loc) · 1.21 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
// Account class that contains a name instance variable
// and methods to set and get its value
public class Account {
private String name; // instance variable
// method to set the name in the object
public void setName(String name) {
this.name(); //store the name
}
// method to retrieve the name from the object
public String getName() {
return name; // return value of name to caller
}
}
// Creating and manipulating an account object
import java.util.Scanner;
public class AccountTest {
public static void main(String[] agrs) {
// create a scanner object to obtain input from the command window
Scanner input = new Scanner(system.in);
// create an account object and assign it to myAccount
Account myAccount = new Account();
// display initial value of (null)
System.out.printf("Initial name is:%s%n%n" , myAccount.getName());
// prompt for and read name
System.out.println("Please enter the name:");
String theName = input.nextLine(); // read a line of text
myAccount.setName(theName); // put thename in myAccount
System.out.println(); // output a blank line
// display the name stored in object myAccount
System.out.printf("Name stored in object myAccount is:%n%s%n" ,
myAcccount.getName());
}
}