-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNullPointerException.java
More file actions
60 lines (47 loc) · 1.35 KB
/
Copy pathNullPointerException.java
File metadata and controls
60 lines (47 loc) · 1.35 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
/******************************************************************************
Write a Java program to create a class called "Dog" with a name and breed
attribute. Create two instances of the "Dog" class, set their attributes using the constructor
and modify the attributes using the setter methods and print the updated values.
*******************************************************************************/
import java.util.*;
class Dog{
private String name;
private int age;
void setName(String name){
this.name=name;
}
void setAge(int age){
this.age=age;
}
String getName(){
return name;
}
int getAge(){
return age;
}
}
class Main{
public static void main(String[] args){
Scanner x=new Scanner(System.in);
System.out.print("Enter Count: ");
int n=x.nextInt();
x.nextLine();
Dog[] dog=new Dog[n];
for(int i=0;i<n;i++){
System.out.print("Enter Dog Name "+(i+1)+": ");
String s=x.next();
x.nextLine();
dog[i].setName(s);
System.out.print("Enter Age: ");
int d=x.nextInt();
x.nextLine();
dog[i].setAge(d);
}
System.out.println("\nDatabase:");
for(int i=0;i<n;i++){
System.out.println("Dog "+(i+1)+" Name: "+dog[i].getName());
System.out.println("Age: "+dog[i].getAge());
System.out.println();
}
}
}