0% found this document useful (0 votes)
87 views10 pages

IT232 - Project

The document provides instructions for a college project assignment with two parts. Part 1 involves writing a Java program to create a basic calculator that takes two numbers as input, allows the user to select an operation, and displays the result. Part 2 involves creating a Student class with attributes like ID, name, GPA and major. Setter, getter and other methods are to be included. A Driver class with a main method is also to be created to demonstrate class functionality.

Uploaded by

wazirkqasem
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)
87 views10 pages

IT232 - Project

The document provides instructions for a college project assignment with two parts. Part 1 involves writing a Java program to create a basic calculator that takes two numbers as input, allows the user to select an operation, and displays the result. Part 2 involves creating a Student class with attributes like ID, name, GPA and major. Setter, getter and other methods are to be included. A Driver class with a main method is also to be created to demonstrate class functionality.

Uploaded by

wazirkqasem
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/ 10

College of Computing and Informatics

Project
Deadline: Day 04/12/2023 @ 23:59
[Total Mark is 14]

Student Details: CRN:


Name: Khalid Ali Mushram ID: S220030639

Instructions:

• You must submit two separate copies (one Word file and one PDF file) using the Assignment Template on
Blackboard via the allocated folder. These files must not be in compressed format.
• It is your responsibility to check and make sure that you have uploaded both the correct files.

• Zero mark will be given if you try to bypass the SafeAssign (e.g. misspell words, remove spaces between
words, hide characters, use different character sets, convert text into image or languages other than English
or any kind of manipulation).

• Email submission will not be accepted.

• You are advised to make your work clear and well-presented. This includes filling your information on the cover
page.

• You must use this template, failing which will result in zero mark.
• You MUST show all your work, and text must not be converted into an image, unless specified otherwise by
the question.
• Late submission will result in ZERO mark.

• The work should be your own, copying from students or other resources will result in ZERO mark.

• Use Times New Roman font for all your answers.


Pg. 01 Description and Instructions

Description and Instructions


Part 1: (5 Marks)

Write a complete Java program that allows users to enter two integer numbers
as follows:

Upon providing the two integer numbers, the users should be given a list of
operations to select from, as follows:

Based on the users’ selection, a method should be called to perform the


selected operation and return the result. After that the main method should
print the result. The output of the program should be as follows:
Pg. 02 Description and Instructions
Pg. 03 Description and Instructions

Part 2: (9 Marks)

1)Create a class Student according to the following requirements:

a) A student has four attributes: id, name, GPA and major.

b) Add the default constructor (no parameters).

c) Add a constructor with four parameters to initialize all the attributes


by specific values.

d) Add all the required setter methods

e) Add all the required getter methods

f) Add the method changeMajor

2) Create a Driver class. that has the main method, in which:

a) Create a student object (obj1), provide all values for the class
attributes.

b) Create another student object (obj2), provide all values for the class
attributes.

c) Print the values of obj1 and obj2 fields.

d) Change the major for obj1.

e) Change the major for obj2.

f) Print the values of obj1 and obj2 fields.


Pg. 04 Description and Instructions

Solution:

To create the Java calculator as given in question. Follow the steps:

Prompt the user to enter the first integer number.Read and store the first
number.Prompt the user to enter the second integer number.Read and store the second
number.Prompt the user to select an operation ('a' for addition, 'b' for multiplication, 'c'
for finding the average).Perform the selected operation and display the result.

Code with Comments:

import java.util.Scanner;

public class Calculator {


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

// Step 1: Prompt the user to enter the first number


System.out.println("Please enter the first number:");
int firstNumber = input.nextInt();

// Step 3: Prompt the user to enter the second number


System.out.println("Please enter the second number:");
int secondNumber = input.nextInt();

// Step 5: Prompt the user to select an operation


System.out.println("Please select one of the following operations by
typing a, b, or c");
System.out.println("--------------------------------------------------
------------------------");
System.out.println("a- Add the two numbers");
System.out.println("b- Multiply the two numbers");
System.out.println("c- Find the average of the two numbers");
System.out.println("--------------------------------------------------
------------------------");
char operation = input.next().charAt(0);

switch (operation) {
case 'a':
// Perform addition
int sum = addNumbers(firstNumber, secondNumber);
System.out.println(firstNumber + " + " + secondNumber + " = " + sum);
break;
case 'b':
// Perform multiplication
int product = multiplyNumbers(firstNumber, secondNumber);
Pg. 05 Description and Instructions

System.out.println(firstNumber + " * " + secondNumber + " = " +


product);
break;
case 'c':
// Calculate and display the average
double average = findAverage(firstNumber, secondNumber);
System.out.println("The average of " + firstNumber + " and " +
secondNumber + " is " + average);
break;
default:
System.out.println("Invalid operation selection.");
}

input.close();
}

// Step 6: Methods to perform operations


public static int addNumbers(int a, int b) {
return a + b;
}

public static int multiplyNumbers(int a, int b) {


return a * b;
}

public static double findAverage(int a, int b) {


return (a + b) / 2.0;
}
}

Explanation

The program starts by taking two integer inputs from the user.The user is then
prompted to select an operation ('a', 'b', or 'c').Depending on the user's choice, one of
the three methods is called to perform the operation.The result is displayed to the user.

Output of the code:


Pg. 06 Description and Instructions

The output is displaying the correct result of the selected operation in the format given
in question.

Part 2, which includes creating a Student class and a Driver class to demonstrate the
functionality as given in the main question:

Student.java:

public class Student {


// a) Attributes
private int id;
private String name;
private double GPA;
private String major;

// b) Default constructor
public Student() {
}
Pg. 07 Description and Instructions

// c) Constructor with four parameters


public Student(int id, String name, double GPA, String major) {
this.id = id;
this.name = name;
this.GPA = GPA;
this.major = major;
}

// d) Setter methods
public void setId(int id) {
this.id = id;
}

public void setName(String name) {


this.name = name;
}

public void setGPA(double GPA) {


this.GPA = GPA;
}

public void setMajor(String major) {


this.major = major;
}

// e) Getter methods
public int getId() {
return id;
}

public String getName() {


return name;
}
Pg. 08 Description and Instructions

public double getGPA() {


return GPA;
}

public String getMajor() {


return major;
}

// f) Method to change major


public void changeMajor(String newMajor) {
this.major = newMajor;
}
}

Driver.java:

public class Driver {


public static void main(String[] args) {
// a) Create a student object (obj1) with initial values
Student obj1 = new Student(1, "Alice", 3.7, "Computer Science");

// b) Create another student object (obj2) with initial values


Student obj2 = new Student(2, "Bob", 3.5, "Engineering");

// c) Print the values of obj1 and obj2 fields


System.out.println("Student 1 Details:");
System.out.println("ID: " + obj1.getId());
System.out.println("Name: " + obj1.getName());
System.out.println("GPA: " + obj1.getGPA());
System.out.println("Major: " + obj1.getMajor());

System.out.println("\nStudent 2 Details:");
System.out.println("ID: " + obj2.getId());
System.out.println("Name: " + obj2.getName());
System.out.println("GPA: " + obj2.getGPA());
System.out.println("Major: " + obj2.getMajor());
Pg. 09 Description and Instructions

// d) Change the major for obj1


obj1.changeMajor("Mathematics");
// e) Change the major for obj2
obj2.changeMajor("Physics");

// f) Print the updated values of obj1 and obj2 fields


System.out.println("\nStudent 1 Details after Major Change:");
System.out.println("ID: " + obj1.getId());
System.out.println("Name: " + obj1.getName());
System.out.println("GPA: " + obj1.getGPA());
System.out.println("Major: " + obj1.getMajor());

System.out.println("\nStudent 2 Details after Major Change:");


System.out.println("ID: " + obj2.getId());
System.out.println("Name: " + obj2.getName());
System.out.println("GPA: " + obj2.getGPA());
System.out.println("Major: " + obj2.getMajor());
}
}

Explanation

The Student class represents a student with attributes (id, name, GPA, major).It
provides a default constructor and a parameterized constructor to initialize the
attributes.Setter and getter methods are used to set and retrieve attribute values.The
changeMajor method allows you to change the student's major.The Driver class
demonstrates the functionality by creating two Student objects, modifying their majors,
and displaying their details before and after the change.

Explanation of the output:

The output will show the details of two students (obj1 and obj2) before and after
changing their majors. This demonstrates that you can create, initialize, and modify
objects of the Student class.

You might also like