ICSE Class 12 Computer Science Question 3 of 3

Solved 2018 Practical Paper ISC Computer Science — Question 3

Back to all questions
3
Question

Question 3

The names of the teams participating in a competition should be displayed on a banner vertically, to accommodate as many teams as possible in a single banner. Design a program to accept the names of N teams, where 2 < N < 9 and display them in vertical order, side by side with a horizontal tab (i.e. eight spaces).

Test your program for the following data and some random data:

Example 1

INPUT:
N = 3
Team 1: Emus
Team 2: Road Rols
Team 3: Coyote

OUTPUT:
E        R       C
m       o       o
u        a        y
s        d        o
                    t
          R        e
          o        
          l        
          s        

Example 2

INPUT:
N = 4
Team 1: Royal
Team 2: Mars
Team 3: De Rose
Team 4: Kings

OUTPUT:
R        M       D       K
o        a         e       i
y        r                  n
a        s          R      g
l                     o      s
                      s      
                      e      

Example 3

INPUT:
N = 10

OUTPUT:
INVALID INPUT

Solution
import java.util.Scanner;

public class Banner
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("ENTER THE VALUE OF N: ");
        int n = in.nextInt();
        in.nextLine();
        
        if (n <= 2 || n >= 9) {
            System.out.println("INVALID INPUT");
            return;
        }
        
        String teams[] = new String[n];
        int highLen = 0;
        
        for (int i = 0; i < n; i++) {
             System.out.print("Team " + (i+1) + ": ");
             teams[i] = in.nextLine();
             if (teams[i].length() > highLen) {
                 highLen = teams[i].length();
             }
        }
        
        for (int i = 0; i < highLen; i++) {
            for (int j = 0; j < n; j++) {
                int len = teams[j].length();
                if (i >= len) {
                    System.out.print(" \t");
                }
                else {
                    System.out.print(teams[j].charAt(i) + "\t");
                }
            }
            System.out.println();
        }
    }
}
Output
BlueJ output of Banner.java
BlueJ output of Banner.java
BlueJ output of Banner.java
Answer

import
java.util.Scanner
;
public
class
Banner
{
public
static
void
main
(
String
args
[]) {
Scanner
in
=
new
Scanner
(
System
.
in);
System
.
out
.
print(
"
ENTER THE VALUE OF N:
"
);
int
n
=
in
.
nextInt();
in
.
nextLine();
if
(n
<=
2
||
n
>=
9
) {
System
.
out
.
println(
"
INVALID INPUT
"
);
return
;
}
String
teams[]
=
new
String
[n];
int
highLen
=
0
;
for
(
int
i
=
0
; i
<
n; i
++
) {
System
.
out
.
print(
"
Team
"
+
(i
+
1
)
+
"
:
"
);
teams[i]
=
in
.
nextLine();
if
(teams[i]
.
length()
>
highLen) {
highLen
=
teams[i]
.
length();
}
}
for
(
int
i
=
0
; i
<
highLen; i
++
) {
for
(
int
j
=
0
; j
<
n; j
++
) {
int
len
=
teams[j]
.
length();
if
(i
>=
len) {
System
.
out
.
print(
"
\t
"
);
}
else
{
System
.
out
.
print(teams[j]
.
charAt(i)
+
"
\t
"
);
}
}
System
.
out
.
println();
}
}
}
Output