-
Notifications
You must be signed in to change notification settings - Fork 2
/
23.1 Object's Array.java
91 lines (74 loc) · 2.63 KB
/
23.1 Object's Array.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Write a program to make "Employee" class consisting of following: -- instance variables --> id & age -- a parameterized constructor to initialize both instance variables.
Create array of "n" Employee objects (where "n" is no. of objects specified by user at run-time) and display the id and age of those employees whose age is less than 30.
Input Format
Program should take the inputs in following sequence: 1) In First input line, no. of Employee objects to create. i.e. value of "n". 2) In remaining input lines, enter id and age values of "n" Employee objects. For example, if no. of Employee-objects to be created are 2, then user-inputs should be as follows: 2 202 31 100 20
Constraints
1) No. of Employee objects range between 1 to 10, i.e. 1 <= n <= 10
2) All id & age values should be positive and range between: 10 <= id <= 1000 ; 18 <= age <= 50
Output Format
If no. of Employee-objects "n" is less than 1, then "Invalid input" should be displayed and no other input should be taken.
If any input value for id & age goes out-of-range (specified in constraints), then display "Invalid data" as overall output. Otherwise, display the id and age of those employees whose age is less than 30, such as follows:
100 20
Sample Input 0
2
202 31
100 20
Sample Output 0
100 20
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Employee
{
int id;
int age;
Employee(int id,int a)
{
this.id = id;
age = a;
}
}
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n>=1 && n<=10)
{
boolean check = true;
Employee []e = new Employee[n];
for(int i=0;i<n;i++)
{
int id = sc.nextInt();
int age = sc.nextInt();
if((id<10 || id>1000) || (age<18 || age>50))
{
check = false;
}
e[i] = new Employee(id,age);
}
if(!check)
{
System.out.print("Invalid data");
}
else
{
for(int i=0;i<n;i++)
{
if(e[i].age<30)
{
System.out.println(e[i].id+" "+e[i].age);
}
}
}
}
else
{
System.out.print("Invalid input");
}
}
}