Computer Applications - Java
Programming Exercises
Question 10
To get promotion in a Science stream, a student must pass in English and should pass in any
of the two subjects (i.e.; Physics, Chemistry or Maths). The passing marks in each subject is
35. Write a program in a Single Dimension Array to accept the roll numbers and marks
secured in the subjects for all the students. The program should check and display the roll
numbers along with a message whether "Promotion is Granted" or "Promotion is not
Granted". Assume that there are 40 students in the class.
import [Link].*;
public class Science
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int ns[] = new int[200]; // 40 * 5 = 200
int i, idx;
[Link]("Enter student details");
for (i = 0, idx = 1; i < 200; i = i + 5, idx++)
{
[Link]("Enter Roll No.: ");
ns[i] = [Link]();
[Link]("Student: " + idx + " Enter English marks:
");
ns[i+1] = [Link]();
[Link]("Student: " + idx + " Enter Maths marks:
");
ns[i+2] = [Link]();
[Link]("Student: " + idx + " Enter Physics marks:
");
ns[i+3] = [Link]();
[Link]("Student: " + idx + " Enter Chemistry
marks: ");
ns[i+4] = [Link]();
}
// Logic for checking promotion
for (i = 0; i < 200; i = i + 5)
{
int roll = ns[i];
int eng = ns[i+1];
int mat = ns[i+2];
int phy = ns[i+3];
int chem = ns[i+4];
int count = 0;
if (mat >= 35) count++;
if (phy >= 35) count++;
if (chem >= 35) count++;
if (eng >= 35 && count >= 2)
[Link]("Roll No: " + roll + " Promotion is
Granted");
else
[Link]("Roll No: " + roll + " Promotion is
not Granted");
}
}
}
Question 18
A class teacher wants to keep the records of 40 students of her class along with their names
and marks obtained in English, Hindi, Maths, Science and Computer Science in a Double
Dimensional Array (DDA) as M[40][5]. When the teacher enters the name of a student as an
input, the program must display the name, marks obtained in the 5 subjects and the total.
Write a program in Java to perform the task.
import [Link].*;
public class Record
{
public static void main(String args[])
{
int ns = 40;
int nsub = 5;
String sub_nm[] = {"English", "Hindi", "Maths", "Science",
"Computer Science"};
Scanner in = new Scanner([Link]);
String names[] = new String[ns];
int marks[][] = new int[ns][nsub];
int i = 0, j = 0;
for (i = 0; i < ns; i++)
{
[Link]("Student " + (i + 1) + " details:");
[Link]("Name: ");
names[i] = [Link]();
for (j = 0; j < nsub; j++)
{
[Link]("Enter marks in " + sub_nm[j] + ": ");
marks[i][j] = [Link]();
}
[Link](); // consume newline
}
[Link]("\nEnter name of the student to search: ");
String stu_nm = [Link]();
for (i = 0; i < ns; i++)
{
if (names[i].equalsIgnoreCase(stu_nm))
break;
}
if (i == ns)
[Link]("Record for " + stu_nm + " not found");
else
{
[Link]("Name: " + names[i]);
int total = 0;
for (j = 0; j < nsub; j++)
{
[Link]("Marks in " + sub_nm[j] + ": " +
marks[i][j]);
total += marks[i][j];
}
[Link]("Total Marks: " + total);
}
}
}
Question 19
If arrays M and M + N are as shown below, write a program in Java to find the array N.
M = [[-1, 0, 2], [-3, -1, 6], [4, 3, -1]] M + N = [[-6, 9, 4], [4, 5, 0], [1, -2, -3]]
public class Find
{
public static void main(String args[])
{
int arrM[][] = {{-1, 0, 2}, {-3, -1, 6}, {4, 3, -1}};
int arrSum[][] = {{-6, 9, 4}, {4, 5, 0}, {1, -2, -3}};
int arrN[][] = new int[3][3];
int i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
arrN[i][j] = arrSum[i][j] - arrM[i][j];
}
}
[Link]("Array N:");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
[Link](arrN[i][j] + " ");
}
[Link]();
}
}
}
Question 20
Write a program in Java to store the numbers in a 4*4 matrix in a Double Dimensional Array
by using an input statement. Now, perform the following tasks:
(a) Display the matrix elements.
(b) Find and display the largest and the smallest elements of the array.
(c) Difference between the largest and the smallest elements of the array.
import [Link].*;
public class ddMax_Min
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int i, j, min, max;
int m[][] = new int[4][4];
[Link]("Enter the elements in the matrix");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
m[i][j] = [Link]();
}
[Link]("Elements of the matrix are:");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
[Link](m[i][j] + " ");
[Link]();
}
min = m[0][0];
max = m[0][0];
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (min > m[i][j]) min = m[i][j];
if (max < m[i][j]) max = m[i][j];
}
}
[Link]("Smallest element in the array is: " + min);
[Link]("Largest element in the array is: " + max);
[Link]("Difference between the smallest and largest
is: " + (max - min));
}
}