0% found this document useful (0 votes)
16 views12 pages

CSC 208 PQ Solution

The document contains 4 programming problems and their solutions in Java. Problem 1 asks to write a program that captures a student's department, year of study, and pays the appropriate prize amount according to a table provided. The total amount paid is also computed. Problem 2 involves reading exam scores of 100 students, displaying them with names, grades, total, and average score. Problem 3 has the user input a number and calls different functions based on if it's positive or negative to multiply the number accordingly. Problem 4 provides class templates, asks to model student-lecturer interaction, and diagrams the Java development process.

Uploaded by

Epaphras Victor
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)
16 views12 pages

CSC 208 PQ Solution

The document contains 4 programming problems and their solutions in Java. Problem 1 asks to write a program that captures a student's department, year of study, and pays the appropriate prize amount according to a table provided. The total amount paid is also computed. Problem 2 involves reading exam scores of 100 students, displaying them with names, grades, total, and average score. Problem 3 has the user input a number and calls different functions based on if it's positive or negative to multiply the number accordingly. Problem 4 provides class templates, asks to model student-lecturer interaction, and diagrams the Java development process.

Uploaded by

Epaphras Victor
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/ 12

CSC 208 PQ Solution

1. FUTA gives prize awards to the best undergraduate students in each


level of study in all its departments. The details of the awards are:
Year of Study Amount
100 Level #1000
200 Level #2000
300 Level #3000
400 Level #4000
500 Level #5000
Using the Select... Case statement, write a java program that captures
the department of the student, year of study and pay him/her a prize as
appropriate, compute and display the total amount paid out to all
beneficiaries of the award.

ANS:

import java.util.Scanner;

public class FUTA_Prize_Awards {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int totalAmountPaid = 0;

System.out.print("Enter the department of the student: ");


String department = input.nextLine();
System.out.print("Enter the year of study: ");
int yearOfStudy = input.nextInt();

switch (yearOfStudy) {
case 100:
totalAmountPaid += 1000;
break;
case 200:
totalAmountPaid += 2000;
break;
case 300:
totalAmountPaid += 3000;
break;
case 400:
totalAmountPaid += 4000;
break;
case 500:
totalAmountPaid += 5000;
break;
default:
System.out.println("Invalid year of study
entered.");
return;
}
System.out.println("The student from the " + department + "
department in " + yearOfStudy + " level will receive a prize of #" +
totalAmountPaid + ".");
input.close();
}
}

2. Write a Java program that reads the names and exam scores of 100
that wrote CSC 208 examination in 2020 and displays a list containing the
names of students, their scores in the courses, grade and at the end of
the list the total and average scores for all the students.

ANS
import java.util.Scanner;

public class CSC208ExamScores {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] names = new String[100];
int[] scores = new int[100];
int totalScore = 0;
for (int i = 0; i < 100; i++) {
System.out.print("Enter the name of student " + (i + 1)
+ ": ");
names[i] = input.nextLine();

System.out.print("Enter the exam score of student " + (i


+ 1) + ": ");
scores[i] = input.nextInt();
input.nextLine();

totalScore += scores[i];
}

double averageScore = (double) totalScore / 100;

System.out.println("\n\nList of students who wrote CSC 208


examination in 2020:");
System.out.println("-----------------------------------------------------
--");

for (int i = 0; i < 100; i++) {


String grade = "";

if (scores[i] >= 70) {


grade = "A";
} else if (scores[i] >= 60) {
grade = "B";
} else if (scores[i] >= 50) {
grade = "C";
} else if (scores[i] >= 45) {
grade = "D";
} else {
grade = "F";
}

System.out.println(names[i] + "\t\t" + scores[i] + "\t\t"


+ grade);
}

System.out.println("-------------------------------------------------------
");
System.out.println("Total score: " + totalScore);
System.out.println("Average score: " + averageScore);
input.close();
}
}
3. Write a Java program that has three functions: main, pos_value and
neg_value. The function main prompts the user for a floating-point
number and checks whether the number is negative or positive. If the
number is negative, main will invoke the function make_pos which
multiplies the number by itself and prints the result. If the number is
positive, main will invoke the function make_neg which multiplies the
number by itself, then by -1 and prints the result.

ANS

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a floating-point number: ");


double number = input.nextDouble();

if (number < 0) {
make_pos(number);
} else {
make_neg(number);
}
input.close();
}

public static void make_pos(double number) {


double result = number * number;
System.out.println("The result is: " + result);
}

public static void make_neg(double number) {


double result = number * number * -1;
System.out.println("The result is: " + result);
}
}

4b. Write down a general template for a Class definition in OOP.


ANS:

public class ClassName {


// Fields (data members)
private int field1;
private String field2;
// Constructors
public ClassName() {
// Default constructor
}
public ClassName(int field1, String field2) {
this.field1 = field1;
this.field2 = field2;
}
// Methods
public void method1() {
// Method 1
}
public int method2(int arg1, String arg2) {
// Method 2
return 0;
}
// Getters and setters
public int getField1() {
return field1;
}
public void setField1(int field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
}

4c. create a UML class diagram representing two objects students and
lecturers in an institution and show the interaction between the objects.

ANS

+----------------+ +----------------+
| Student | | Lecturer |
+----------------+ +----------------+
| -studentID: int| | -lecturerID: int|
| -name: String | | -name: String |
| -email: String | | -email: String |
+----------------+ +----------------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
v v
+----------------+ +----------------+
| CourseGrade | | CourseGrade |
+----------------+ +----------------+
| -studentID: int| | -lecturerID: int|
| -courseID: int | | -courseID: int |
| -grade: double | | -grade: double |
+----------------+ +----------------+

4d. Write an appropriate diagram describe the java program


development process
ANS
+----------------+ +----------------+ +----------------+
| Edit | | Compile | | Load |
+----------------+ +----------------+ +----------------+
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
v v v
+----------------+ +----------------+ +----------------+
| Verify | | Execute | | Deploy |
+----------------+ +----------------+ +----------------+

You might also like