-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61_Constructor6.java
More file actions
56 lines (52 loc) · 1.51 KB
/
61_Constructor6.java
File metadata and controls
56 lines (52 loc) · 1.51 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
/*
* Program for parameterised constructor taking input from user
*/
package javaprograms;
import java.io.*;
/**
*
* @author Geeky Keshav
*/
public class Constructor6
{
static int id; //variable should be declared as static to use it in 'main method'
static String name;
Constructor6 (int i,String n)
{
id=i;
name=n;
}
void disp()
{
System.out.println("Roll no. "+ id+","+" Name: "+name);
System.out.println("++++++++++++++++++++");
}
public static void main(String [] args)throws Exception
{
DataInputStream obj=new DataInputStream(System.in);
System.out.print("Enter Roll no.: ");
id=Integer.parseInt(obj.readLine());
System.out.print("Enter name: ");
name=obj.readLine();
Constructor6 obj1=new Constructor6(id,name); //directly enter the variable name in the parameter section
obj1.disp();
System.out.println("=====================");
System.out.print("Enter Roll no.: ");
id=Integer.parseInt(obj.readLine());
System.out.print("Enter name: ");
name=obj.readLine();
Constructor6 obj2=new Constructor6(id,name);
obj2.disp();
}
}
/********OUTPUT******
Enter Roll no.: 1
Enter name: Geeky
Roll no. 1, Name: Geeky
++++++++++++++++++++
=====================
Enter Roll no.: 2
Enter name: Keshav
Roll no. 2, Name: Keshav
++++++++++++++++++++
*/