Activity 7 - Padawan & Pahl BSM-CS 2C G2 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Activity Problem: Use the week8.pdf and Chapter 4 Lesson1_Arrays powerpoint.

Inside the powerpoint do the Practice Exercise, named it Activity_7. The Activity_7 is
still by pair.
Activity Code:
package activity_7_v2;
import java.io.*; import java.text.DecimalFormat;
public class Activity_7_v2 {
public static void main(String[] args) throws IOException {

/*First, we've prepare all of our variables that we will use on our program*/

InputStreamReader reader = new InputStreamReader(System.in);


BufferedReader input = new BufferedReader(reader);
DecimalFormat oneDcPt = new DecimalFormat("0.0");
int Students, Subjects;

/*Next, we've used "do while" loop for the Restriction 1 and 2 for extra feature
of our program, where every time the user will input an integer less than 0
it will notify the user that the entry was invalid and it will loop back
from the start of the program*/

do{

/*Next, we've put the "input statements" inside the "do while" loop alongside
with some "if" statements*/

//Restriction #1
System.out.print("Enter a number of Student/s: ");

Name/s: Date: 3 / 18 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Students = Integer.parseInt(input.readLine());

//Restriction #2
System.out.print("Enter a number of Subject/s: ");
Subjects = Integer.parseInt(input.readLine());
if(Students > 0 && Subjects > 0){
break;
}
else{
System.out.println("Invalid Entry! Please Try Again.");
System.out.println();
}
}while(!(Students > 0 && Subjects > 0));

/*Subsequently, we've created some arrays for the Student's Name,


Subjects, Grades, and Average Grade*/

String [] Name = new String [Students];


String [] Subject = new String [Subjects];
int [][] Grade = new int [Students][Subjects];
float [] Average = new float [Students];
System.out.println("----------------------------------------------------------");
System.out.println();

/*Afterwards, we've used "for loop", inside of the loop is where the
"input statements" are located. For the condition inside the "for loop"
we've used the user defined inputs for the loop*/

Name/s: Date: 3 / 18 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
//Restriction #3
System.out.println("Enter Subject/s Names: ");
System.out.println();
for(int c = 0; c < Subjects; c++){
System.out.print((c+1) +". ");
Subject [c] = input.readLine();

}
System.out.println();
System.out.println("----------------------------------------------------------");
System.out.println();
for(int a = 0; a < Students; a++){
float sum = 0;
System.out.print("Name: ");
Name [a] = input.readLine();
for(int b = 0; b < Subjects; b++){

//Restriction #2
do{
System.out.print("Grade in " +Subject[b] +": ");
Grade [a][b] = Integer.parseInt(input.readLine());
if(Grade[a][b] >= 1 && Grade[a][b] <= 100){
break;
}
else{
System.out.println();

Name/s: Date: 3 / 18 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
System.out.println("Invalid Entry! Please Try Again.");
System.out.println();
}
}while(!(Grade[a][b] >= 1 && Grade[a][b] <= 100));
sum = Grade[a][b] + sum;
}

/*For the elements of the Avarage array, we've used the Quotient
of the sum and the Subject, and we also introduce an new variable with
a data type of "float", this is also equal to the quotient of the sum and
subjects, we've used "float" so it can show it's decimal value when
it is printed*/

//Restriction #4
Average [a] = sum/Subjects;
float Av = 0; Av = sum/Subjects;
System.out.println("Average Grade: " +oneDcPt.format(Av));
System.out.println();
}

/*Then, we've used "if statement" for an extra feature where when the user
input a single student, then it will only print the general average of the
single student, this is located at the "else" of the "if statement"*/

//Restriction #5
if (Students != 1){

Name/s: Date: 3 / 18 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
/*Here, we can see that we've introduce another variables with a data type
"float", using float we can properly printed out some decimal values,
with the limitation of One Decimal Point prompted above the codes.
Using the length of the array, can compare the elements of array to check
what is the highest average or the lowest average among the students*/

float high = Average[0], low = Average[0];


for(int h = 1; h < Average.length; h++){

/*Also, we can observe that the value of the variable "high" is


equal to the element under Average array [0]. Subsequently, we can
observe that the condition of the "for loop" is prompted to start
the the second element of the array (Average [1]), thru this we can
compare every single element of the array with the help of the incrament
inside the "for loop", until the statement can be satisfy*/

if(high < Average[h]){


high = Average[h];
}
}
for(int l = 1; l < Average.length; l++){
if(low > Average[l]){
low = Average[l];
}
}
System.out.println("----------------------------------------------------------");
System.out.println();

Name/s: Date: 3 / 18 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
System.out.println("Average Grades: "); System.out.println();
System.out.println("\tHighest: " +oneDcPt.format(high));
System.out.println("\tLowest: " +oneDcPt.format(low)); System.out.println();
System.out.println("----------------------------------------------------------");
System.out.println();
}
else{
System.out.println("----------------------------------------------------------");
System.out.println();
System.out.println("Average Grade of Student: "); System.out.println();
System.out.println("\tGrade: " +oneDcPt.format(Average[0]));
System.out.println();
System.out.println("----------------------------------------------------------");
System.out.println();
}

/*Thereafter, we've used the "for loop" again, in order to access the grades
of the Students using the Two Dimentional array, we've used this type of array
previously above this code at the Grade array, by using two dimentional array
we can access the Grades of different students easily, with the help of
"for loop"*/

//Restriction #6
System.out.println("Grade Chart:"); System.out.println();
System.out.println("Subject Name\t\tNo. of Students who Passed");
System.out.println();

/*By analyzing the code, we can see that we've used the 1st "for loop"

Name/s: Date: 3 / 18 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
with the value of the Subjects under its condition, and at the 2nd "for loop"
with the value of the Students*/

for (int p = 0; p < Subjects; p++){


System.out.print(Subject[p] +"\t\t\t");
for(int f = 0; f < Students; f++){

/*Inside, F represents the Students and P represents the Subjects


in the Two dimentional Array Grade[f][p]. Carefully understand the
logic of this type of array, to successfully perform the action.
In Addition, we've used "if statement" to find the passing grade
by comparing all the elements with an integer greater than 70.*/

if(Grade[f][p] >= 70){


System.out.print("*");
}
}
System.out.println();
}
System.out.println();
System.out.println("----------------------------------------------------------");
System.out.println();

/*Finally, resembling to Restriction #6, by comparing the two, we can see


some similarity by the used of Two dimentional array and "For loop"*/

//Restriction #7
System.out.println("Summary Records:"); System.out.println();
Name/s: Date: 3 / 18 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
System.out.println("Name\t\t\tSubject\t\t\tGrade\t\t\tRemarks"); System.out.println();
for(int f = 0; f < Students; f++){
for (int s = 0; s < Subjects; s++){

/*Inside, we can observe that, f represents the Name[f] array,


s represents the Subjects[s], and Grade[f][s] is reprsented by both
variables. We've also used "if statement" to identify which
Subjects are passed by the students.*/

System.out.print(Name[f] +"\t\t\t" +Subject[s] +"\t\t\t");


System.out.print(Grade[f][s] + "\t\t\t");
if(Grade[f][s] >= 70){
System.out.println("Passed");
}
else{
System.out.println("Failed");
}
}
System.out.println("---------------------------------------------------------------------------------
----");
}
}

Name/s: Date: 3 / 18 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Program:

Name/s: Date: 3 / 18 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Name/s: Date: 3 / 18 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Name/s: Date: 3 / 18 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Output:

Name/s: Date: 3 / 18 / 2020


Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres
Name/s: Date: 3 / 18 / 2020
Padawan, Reggie C.
Pahl, George Christopher A.
Activity Number: 7
Section: BSM – CS – 2C Instructor: Mrs. Robethel Andres

You might also like