-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayOutLine .java
More file actions
153 lines (137 loc) · 3.94 KB
/
ArrayOutLine .java
File metadata and controls
153 lines (137 loc) · 3.94 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package array;
import java.util.*;
public class ArrayOutLine {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//Asking the user name
System.out.println("Hi! Please Enter your Name: ");
String name = scan.nextLine();
//Ask the user what operation he wants to perform by giving the user the options
System.out.println("Hi "+name+" Please select what you want enter from below options: ");
System.out.println("1.Enter the ages details ");
System.out.println("2.Enter the classroom and age details");
System.out.println("3.Enter the school,classroom and age details of the student");
//Take the input and call the required method which implemented in the Arrayimplement class
ArrayImplementaion ai=new ArrayImplementaion();//object of Arrayimplement class
int choice = scan.nextInt();
if(choice == 1)
{
ai.oneDArr();
}
else if(choice ==2)
{
ai.twoDArr();
}
else if(choice ==3)
{
ai.threeDArr();
}
}
}
class ArrayImplementaion
{
void oneDArr()
{
Scanner scan = new Scanner(System.in);
//Ask the no of ages(size of the array) the user wants
System.out.println("Enter the no of ages deatils you want to enter: ");
int n=scan.nextInt();
int[] a=new int[n];
//taking ages from user
for(int i=0;i<a.length;i++)
{
System.out.println("Enter the "+(i+1)+" age");
a[i]=scan.nextInt();
}
//displaying the ages
System.out.println("The ages of the students are: ");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
void twoDArr()
{
Scanner scan = new Scanner(System.in);
//First ask the user for the no of classrooms(size of the row)
System.out.println("Enter the no of classrooms: ");
int r=scan.nextInt();
int[][] a=new int[r][];
//now ask the no of student ages(size of the colums) for each classroom(row)
for(int i=0;i<r;i++)
{
System.out.println("enter the no of students in "+(i+1)+" classroom:");
a[i]=new int[scan.nextInt()];
}
//now the array is created for classroom and students present in
//ask the user to enter the deatils
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
System.out.println("Enter the classroom "+(i+1)+" student "+(j+1)+" age: ");
a[i][j]=scan.nextInt();
}
}
//display the details
System.out.println("The ages are");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
void threeDArr()
{
Scanner scan = new Scanner(System.in);
//ask the user for the no of schools(size of the blocks)
System.out.println("Enter the no of schools:");
int b = scan.nextInt();
int[][][] a=new int[b][][];
//now ask the no of classrooms(no of rows) present in each school
for(int i=0;i<a.length;i++) {
System.out.println("enter the no of classrooms in school "+(i+1)+" :");
a[i]=new int[scan.nextInt()][];
}
//finally ask the the no of students(no of columns) in each class
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
System.out.println("enter the no of students in classroom "+(j+1)+" school "+(i+1));
a[i][j]=new int[scan.nextInt()];
}
}
//now the array is created
//ask the user to enter the age details
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
for(int k=0;k<a[i][j].length;k++)
{
System.out.println("Enter the age of student "+(k+1)+" of classroom "+(j+1)+" in school "+(i+1) );
a[i][j][k]=scan.nextInt();
}
}
}
//display the details
System.out.println("The ages are: ");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
for(int k=0;k<a[i][j].length;k++)
{
System.out.print(a[i][j][k]+" ");
}
System.out.println();
}
System.out.println();
System.out.println();
}
}
}