0% found this document useful (0 votes)
20 views8 pages

Class Test 2 - Memo

The document outlines a class test for Programming II, including instructions for students and a series of programming tasks related to creating and managing a TestScores class. It details the implementation of methods for setting and getting test scores, displaying default scores, and handling user input for modules and scores. Additionally, it includes the creation of a new class and application to further manage and display student marks linked to specific modules.
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)
20 views8 pages

Class Test 2 - Memo

The document outlines a class test for Programming II, including instructions for students and a series of programming tasks related to creating and managing a TestScores class. It details the implementation of methods for setting and getting test scores, displaying default scores, and handling user input for modules and scores. Additionally, it includes the creation of a new class and application to further manage and display student marks linked to specific modules.
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/ 8

Department of Information and Communication Technology

COURSE NAME: Programming II

COURSE CODE: NPRG62120

Class Test 2 Name of Examiner


Date: 25 June 2021
Mrs. K.E Mamabolo
Total Marks: 69
I declare that I am familiar Duration: 3h00
with and will abide by the Total pages: 3 (including
Assessment rules as
indicated in the Sol Plaatje cover)
University Rules Book

Name of Moderator

Dr. Tite Tuyikeze

_____________________

Signature Student number


Surname Initials
%

IMPORTANT INSTRUCTIONS

1. Answer ALL questions.


2. Use the spaces provided to answer each question.
3. The use of Calculator is not permitted.
4. The use of laptops is permitted.
5. The use of Cellphones is not permitted.
Question 1

a) Create a class called TestScores that keeps a record of student tests. The class should have the
two data field that declares an array as testScores and initialize it to {60, 50, 67}, the moduleName
to capture the module names. Within the TestScore class include the following:
Declaration and initialization of TestScore Array: (2)
private int[] testScores = {60,55,67};√√
 A set and get method for the testScores and moduleName (12)

Solution:

public String getModuleName() {√

return moduleName; √

public void setModuleName(String moduleName) {√

this.moduleName = moduleName; √

public void setTestScores(int number, int score){ √√

testScores[number] = score; √√

public int getTestScores(int number){ √√

return testScores[number]; √√

 A method that will display the default test scores. (5)

Solution:

public void display(){√

System.out.print("The defaul test scores are: ");

for(int i = 0; i < testScores.length; ++ i) √√

System.out.print(testScores[i] + " ");√√


}

b) Create an application known as TestScoreDemo that displays the default test scores. (4)

Solution:

TestScores module = new TestScores();√√

module.display();√√

c) Amend the application you created in question 1b) to include the following:
1. Declare an object that allows the user to add two modules that the student marks will be
linked to. (2)

Solution:

TestScores[] modules = new TestScores[NUM_MODULES]; √√

2. Prompt the user to input two modules (NPRG62120 and NITE62110) and the student marks
for each module. (12)
o NB: The test scores should be linked to an individual module. For instance, the test
scores for NPRG62120 are {80, 65, 55} and NITE62110 are {55, 68, 89}.

Solution:

for(y = 0; y < NUM_MODULES; ++y){ √√

modules[y] = new TestScores();√√

System.out.println("Enter the module name: ");

name = input.nextLine();

modules[y].setModuleName(name); √√

for(x = 0; x < NUM_TEST_SCORES; ++x){ √√

System.out.println("Enter the student marks: ");

score = input.nextInt();

modules[y].setTestScores(x, score); √√

input.nextLine();√√

}
}

3. Display the student marks per module as captured in question c2). (8)

Solution:

for(y = 0; y < NUM_MODULES; ++y){ √√

System.out.print("\nModule name is: " + modules[y].getModuleName());√√

for (x = 0; x < NUM_TEST_SCORES; ++x) √√

System.out.print(" " + modules[y].getTestScores(x)+ " ");√√

System.out.println();

4. Include the segment of code that will allow the user to search for a particular module and the
program should respond by displaying the module and the marks of the students linked to the
module. (10)

Solution:

System.out.println("\n\nEnter a module name that you want to search >> ");½

name = input.nextLine();½

for(y = 0; y < modules.length; ++y){ √√

if(name.equals√(modules[y].getModuleName()))√√

for(x = 0; x < NUM_TEST_SCORES; ++x){ √√

System.out.println(modules[y].getTestScores(x)+ ""); √√

System.out.println();

d) Create a new class called TestScore1 and copy the segments of code in TestScore to the newly
created class TestScore1. (4)
o Within TestScore1, move the segments of code in question c (1- 4) and store them into a
nonstatic method called getTestMarksData that receives a TestScore1 parameter. (6)

Solution:
package testscoresjune;

import java.util.Scanner;

/**

* @author Eva.Mamabolo

*/

public class TestScore1 { [4] Creating a new class and copy the copy across

private String moduleName;

private int[] testScores = {60,55,67};

public String getModuleName() {

return moduleName;

public void setModuleName(String moduleName) {

this.moduleName = moduleName;

public void setTestScores(int number, int score){

testScores[number] = score;

public int getTestScores(int number){

return testScores[number];

public void display(){

System.out.print("The defaul test scores are: ");


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

System.out.print(testScores[i] + " ");

public void getTestMarksData √√ (TestScore1 scores){ √√ (6)


2 Marks to copy the code across

final int NUM_MODULES = 2;

final int NUM_TEST_SCORES = 2;

String name;

int score;

TestScores module = new TestScores();

module.display();

System.out.println(" ");

TestScores[] modules = new TestScores[NUM_MODULES];

int x, y;

Scanner input = new Scanner(System.in);

for(y = 0; y < NUM_MODULES; ++y){

modules[y] = new TestScores();

System.out.println("Enter the module name: ");

name = input.nextLine();

modules[y].setModuleName(name);

for(x = 0; x < NUM_TEST_SCORES; ++x){

System.out.println("Enter the student marks: ");

score = input.nextInt();

modules[y].setTestScores(x, score);
input.nextLine();

for(y = 0; y < NUM_MODULES; ++y){

System.out.print("\nModule name is: " + modules[y].getModuleName());

for (x = 0; x < NUM_TEST_SCORES; ++x)

System.out.print(" " + modules[y].getTestScores(x)+ " ");

System.out.println();

System.out.println("\n\nEnter a module name that you want to search >> ");

name = input.nextLine();

for(y = 0; y < modules.length; ++y){

if(name.equals(modules[y].getModuleName()))

for(x = 0; x < NUM_TEST_SCORES; ++x){

System.out.println(modules[y].getTestScores(x)+ "");

System.out.println();

e) Create a new application called TestScore1Data that will display the student marks. (4)
Solution:
public class TestScoreJune1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

TestScore1 module = new TestScore1();√√


module.getModuleData(module); √√

You might also like