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

Object Oriented Software Concepts and Development PRACTICAL ASSIGNMENT N0236685B

The document contains a practical assignment for a Java programming course, requiring students to create a program that calculates the average and highest marks for 40 students, as well as a login application that authenticates users against a MySQL database. The first part includes methods for capturing marks, calculating their sum, and determining the highest mark. The second part outlines the creation of a user authentication system with a graphical interface and database interactions for login functionality.

Uploaded by

kudzai radmen
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)
21 views10 pages

Object Oriented Software Concepts and Development PRACTICAL ASSIGNMENT N0236685B

The document contains a practical assignment for a Java programming course, requiring students to create a program that calculates the average and highest marks for 40 students, as well as a login application that authenticates users against a MySQL database. The first part includes methods for capturing marks, calculating their sum, and determining the highest mark. The second part outlines the creation of a user authentication system with a graphical interface and database interactions for login functionality.

Uploaded by

kudzai radmen
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

FACULTY OF APPLIED SCIENCES

DEPARTMENT OF COMPUTER SCIENCE

SCS 2108 Object Oriented Software Concepts & Development

Object Oriented Software Concepts and Development


SCS 2108
KUDZAISHE RADMEN
N0236685B
COMPUTER SCIENCE
PRACTICAL ASSIGNMENT
Question 1

Write a complete Java program that accept a set of marks in test out of 100 for 40
students. The program should find the average and highest marks in the test and
display them. Use a method to capture the marks into an array via keyboard. Include
another method that receive an array of the captured marks and returns their sum.
[20]

import java.util.Scanner;

public class App {


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

// enter number of students in the class


System.out.print("Enter number of students in the class:");
int NUM_STUDENTS = scanner.nextInt();

// Capture marks into an array


int[] marks = captureMarks(NUM_STUDENTS);

// Calculate the sum of marks


int totalMarks = calculateSum(marks);

// Calculate the average


double average = totalMarks / (double) NUM_STUDENTS;

// Find the highest marks


int highestMarks = findHighestMarks(marks);

// Display results
System.out.println("Average marks: " + average);
System.out.println("Highest marks: " + highestMarks);
}

// Method to capture marks from the keyboard


public static int[] captureMarks(int numStudents) {
Scanner scanner = new Scanner(System.in);
int[] marks = new int[numStudents];

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


System.out.print("Enter marks for student " + (i + 1) + ": ");
marks[i] = scanner.nextInt();

// Ensure marks are within 0-100


while (marks[i] < 0 || marks[i] > 100) {
System.out.println("Invalid input. Marks must be between 0 and
100.");
System.out.print("Enter marks for student " + (i + 1) + ": ");
marks[i] = scanner.nextInt();
}
}

return marks;
}

// Method to calculate the sum of the array


public static int calculateSum(int[] marks) {
int sum = 0;
for (int mark : marks) {
sum += mark;
}
return sum;
}

// Method to find the highest marks in the array


public static int findHighestMarks(int[] marks) {
int highest = marks[0];
for (int mark : marks) {
if (mark > highest) {
highest = mark;
}
}
return highest;
}

}
Output of example using a class of 40 students including of example of what happens
when mark not within the range is entered
Question 2

Create a login Java application that authenticate users by checking their details in a
database with an interface given below. The application should allow the user to enter
their username and password. On clicking login button, if user credentials are in the
database the application should display a message “Log in Successful” else it displays
message “Wrong password or username”. On clicking reset button the application
should clear the username and password textboxes. Create MySQL database with 3
user details which the application works with for user authentication.
[30]

MYSQL code to create Database

CREATE DATABASE user_auth;

USE user_auth;

CREATE TABLE users (

id INT AUTO_INCREMENT PRIMARY KEY,

username VARCHAR(50) NOT NULL UNIQUE,

password VARCHAR(50) NOT NULL

);

INSERT INTO users (username, password) VALUES

('john_doe', 'password123'),

('jane_smith', 'mypassword'),

('admin', 'admin123');
Code to create and implement login page,

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class App { private static Connection connect() {


String url = "jdbc:mysql://localhost:3306/user_auth";
String user = "root"; // MySQL username
String password = ""; // MySQL password

// Connecting to database
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Database connection failed!");
return null;
}
}

public static void main(String[] args) {


// login page creation
JFrame frame = new JFrame("Login Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setLayout(new GridLayout(4, 2));

JLabel usernameLabel = new JLabel("Username:");


JTextField usernameField = new JTextField();

JLabel passwordLabel = new JLabel("Password:");


JPasswordField passwordField = new JPasswordField();

JButton loginButton = new JButton("Login");


JButton resetButton = new JButton("Reset");

frame.add(usernameLabel);
frame.add(usernameField);
frame.add(passwordLabel);
frame.add(passwordField);
frame.add(loginButton);
frame.add(resetButton);
frame.setVisible(true);

loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());

Connection conn = connect();


if (conn != null) {
try {
String query = "SELECT * FROM users WHERE username = ? AND
password = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, password);

ResultSet rs = stmt.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(frame, "Login
Successful");
} else {
JOptionPane.showMessageDialog(frame, "Wrong username
or password");
}

stmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
});

resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
usernameField.setText("");
passwordField.setText("");
}
});
}
}
OUTPUT

Login page

Database connection error

Wrong password or username error message


Correct username and password

You might also like