0% found this document useful (0 votes)
22 views7 pages

Laboratory Activity #10

This document contains source code for a Java program that generates and analyzes a 2D array of multiples of 5. The program: 1) Creates a 2D integer array and populates it with multiples of 5 based on the row and column. 2) Randomizes the values in the array by swapping random elements. 3) Prints the original and randomized arrays. 4) Searches the randomized array and prints the indexes of elements with values of 50, 330, and 450.

Uploaded by

Danny Clemente
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Laboratory Activity #10

This document contains source code for a Java program that generates and analyzes a 2D array of multiples of 5. The program: 1) Creates a 2D integer array and populates it with multiples of 5 based on the row and column. 2) Randomizes the values in the array by swapping random elements. 3) Prints the original and randomized arrays. 4) Searches the randomized array and prints the indexes of elements with values of 50, 330, and 450.

Uploaded by

Danny Clemente
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Name: Clemente, Danielle Rose Date: June 8, 2021

CITE 003 / CS12S1 / M W F / 8:30am - 10:30am Professor: Mr. Alfio Regla

Laboratory Activity #10

 
Source Code (screenshot):
Text-based Code:

package finalsactivities;

public class RandomizedTwoDimensionalArray {

public static void main(String[] args) {

/** For title purposes and design


* The little box looking filled with symbols are just,
* for esthetic purposes only.
*/
System.out.println("\s_______________________________________________________________________________");
System.out.println("|-----------------RANDOMIZED ARRAY AND TRACKING SPECIFIC VALUES-----------------|");
System.out.println("|_______________________________________________________________________________|");
System.out.println("+-------------------------------------------------------------------------------+");
System.out.println("|\t\t\tORIGINAL TABLE OF MULTIPLES OF 5\t\t\t|");

//To create the 2D array


int[][] table = new int [10][10];
int value;

for(int row = 0; row < table.length; row++) {


if (row == 0) {
System.out.printf("|_______________________________________________________________________________|\n"); //To
enclose the title

}
for(int column = 0; column < table[row].length; column++) {
value = (row * 50) + ((column + 1) * 5); //To get the next number depending on
the row and column
table[row][column] = value; //To hold the value computed
if (column == 0) {
System.out.printf("|"); //To put a line before each
value placed on the leftmost

}
System.out.printf("\s\s" + table[row][column] + "\t|"); //To display value and make it not
close to dividers

}
System.out.println("");
System.out.println("+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+"); //For enclosing
purposes as part of the design
}

System.out.println("|\t\t\t\t\t\t\t\t\t\t|");
for(int i= 0; i < table.length ; i++) {
for(int j = 0; j < table[i].length; j++) {
int i1 = (int)(Math.random() * table.length); //To pick any number from 0-9 for i1
int j1 = (int)(Math.random() * table[i].length); //To pick any number from 0-9 for j1
//To swap matrix [i][j] with matrix [i1][i1j]
int temporaryVal = table[i][j];
table[i][j] = table[i1][j1];
table[i1][j1] = temporaryVal;
}
}

System.out.println("+-------------------------------------------------------------------------------+");
System.out.println("|\t\t\s\s\s\s\s\s\sRANDOMIZED TABLE OF MULTIPLES OF 5\t\t\t|");
for(int row = 0; row < table.length ; row++) {
if (row == 0) {
System.out.printf("|_______________________________________________________________________________|\n"); //To
enclose the title
}
for(int column = 0; column < table[row].length; column++) {
if (column == 0) {
System.out.printf("|");
//To put a line before each value placed on the leftmost

}
System.out.printf("\s\s" + table[row][column] + "\t|"); //To
display value and make it not close to dividers

}
System.out.println("");
System.out.println("+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+"); //For enclosing
purposes as part of the design
}

System.out.println("|\t\t\t\t\t\t\t\t\t\t|");
System.out.println("+-------------------------------------------------------------------------------+");
System.out.println("|\t\t\t\s\s\sINDEX OF 50, 330, AND 450.\t\t\t\t|");
System.out.println("|_______________________________________________________________________________|");
System.out.println("|\t\t\t\t\t\t\t\t\t\t|");

for(int i = 0; i < table.length; i++) {


for(int j = 0; j < table[i].length; j++) {
if (table[i][j] == 50) {
System.out.println("|\t\t\s\sThe value 50 is currently on index [" + i + "][" + j + "]\t\t\t|");
//To print the index for the value 50
} else if (table[i][j] == 330) {
System.out.println("|\t\t\s\sThe value 330 is currently on index [" + i + "][" + j + "]\t\t\t|");
//To print the index for the value 330
} else if (table[i][j] == 450) {
System.out.println("|\t\t\s\sThe value 450 is currently on index [" + i + "][" + j + "]\t\t\t|");
//To print the index for the value 450
}
}
}

System.out.printf("|_______________________________________________________________________________|\n"); //To enclose the end


design

}
Sample Output:
Source Code (screenshot):
Text-based Code:

package finalsactivities;

public class GradingAMultipleChoiceTest {

public static void main(String[] args) {


String [][] answers = { //Array for all the answers of all the
students
{"A","B","A","C","C","D","E","E","A","D"},
{"D","B","A","B","C","A","E","E","A","D"},
{"E","D","D","A","C","B","E","E","A","D"},
{"C","B","A","E","D","C","E","E","A","D"},
{"A","B","D","C","C","D","E","E","A","D"},
{"B","B","E","C","C","D","E","E","A","D"},
{"B","B","A","C","C","D","E","E","A","D"},
{"E","B","E","C","C","D","E","E","A","D"},
};

String [] answerKey = {"D","B","D","C","C","D","A","E","A","D"}; //To declare the given correct answers


to the questions
int[] scores = {0, 0, 0, 0, 0, 0, 0, 0}; //To initialize the scores for each
student

/** For title purposes and design


* The little box looking filled with symbols are just,
* for esthetic purposes only.
*/
System.out.println("\s_______________________________________________________________");
System.out.println("|--------------- Grading A Multiple Choice Test ----------------|");
System.out.println("|_______________________________________________________________|");
System.out.println("+---------------------------------------------------------------+");
System.out.println("|\t\t\tStudents' Answers\t\t\t|");
System.out.println("|_______________________________________________________________|");
System.out.println("|\t\t\t\t\t\t\t\t|"); //To connect
different categories to make it look inside 1 main rectangle
System.out.println("|\t\t\t\s\s\s\s\s0 1 2 3 4 5 6 7 8 9\t\t|"); //To put order for
each answer of students

for(int i = 0; i < answers.length; i++) {


System.out.printf("|\t\t\sStudent " + i + " : " ); //To label
the students from 0-9
for(int j = 0; j < answers[i].length; j++) {
System.out.printf(answers[i][j] + " "); //To
print students' answers from the array named answers
}
System.out.printf("\t\t|\n"); //To enclose the
box after students' answers
}
System.out.println("|\t\t\t\t\t\t\t\t|"); //To connect
different categories to make it look inside 1 main rectangle
System.out.println("|_______________________________________________________________|");
System.out.println("|\t\t\t\t\t\t\t\t|"); //To connect
different categories to make it look inside 1 main rectangle
System.out.println("+---------------------------------------------------------------+");
System.out.println("|\t\t\s\sStudents' Scores with Percentage\t\t|");
System.out.println("|_______________________________________________________________|");

for(int i = 0; i < answers.length; i++) {


for(int j = 0; j < answers[i].length; j++) {
if (answers[i][j].equals(answerKey[j])) { //To
increment score of a student if his or her answer is correct based on the answer key
scores[i]++;
}
}
System.out.println("|\t\t\t\t\t\t\t\t|"); //To connect
different categories to make it look inside 1 main rectangle

//To print label of student, their corresponding scores, and its percentage.
System.out.printf("|\sStudent %d: correct answers count is %d \t\s\s(%.2f %%)\t|\n", i,
scores[i], (double) scores[i] / answerKey.length * 100 );
}

System.out.println("|_______________________________________________________________|");

System.out.printf("|-------------- Answer Key: "); //Part of the


design to make it look like the header
for(int i = 0; i < answerKey.length; i++) { //To display the
given answer key
System.out.printf(answerKey[i] + " ");
}
System.out.printf("----------------|");
System.out.println();
System.out.println("|_______________________________________________________________|");
}

}
Sample Output:

” I affirm that I have not given or received any unauthorized help in this
assignment, and that this work is my own “

You might also like