[go: up one dir, main page]

0% found this document useful (0 votes)
12 views6 pages

Number 1

The document contains three Java programs. The first program is a grading system that calculates the average and grade based on user input for five subjects. The second program is a shopping application that allows users to select products and calculate total prices, while the third program displays member information for a SACCO based on user-provided member IDs.

Uploaded by

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

Number 1

The document contains three Java programs. The first program is a grading system that calculates the average and grade based on user input for five subjects. The second program is a shopping application that allows users to select products and calculate total prices, while the third program displays member information for a SACCO based on user-provided member IDs.

Uploaded by

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

Number 1

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

public class Grading extends JFrame {


private JTextField[] subjectMarks;
private JButton calculateButton;
private JTextArea resultArea;

public Grading() {
// Set up the frame
setTitle("Mida Vocational Institute Grading System");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());

// Initialize components
subjectMarks = new JTextField[5];
for (int i = 0; i < 5; i++) {
subjectMarks[i] = new JTextField(5);
add(new JLabel("Subject " + (i + 1) + ":"));
add(subjectMarks[i]);
}
calculateButton = new JButton("Calculate Grade");
resultArea = new JTextArea(5, 30);
resultArea.setEditable(false);
// Add action listener to the button
calculateButton.addActionListener(new CalculateGradeAction());

add(calculateButton);
add(new JScrollPane(resultArea));
}

private class CalculateGradeAction implements ActionListener {


@Override
public void actionPerformed(ActionEvent e) {
int totalMarks = 0;
int subjectCount = 0;

for (JTextField markField : subjectMarks) {


String input = markField.getText();
if (!input.isEmpty()) {
try {
int marks = Integer.parseInt(input);
if (marks >= 0 && marks <= 100) {
totalMarks += marks;
subjectCount++;
} else {
JOptionPane.showMessageDialog(null, "Marks must be
between 0 and 100");
return;
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid input: " +
input);
return;
}
}
}

if (subjectCount == 0) {
JOptionPane.showMessageDialog(null, "Please enter marks for at
least one subject");
return;
}

double average = (double) totalMarks / subjectCount;


String grade = calculateGrade(average);
resultArea.setText("Average: " + String.format("%.2f", average) + "\
nGrade: " + grade);
}

private String calculateGrade(double average) {


if (average >= 85) return "A";
else if (average >= 75) return "B";
else if (average >= 65) return "C";
else if (average >= 50) return "D";
else return "F";
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
Grading gradingApp = new Grading();
gradingApp.setVisible(true);
});
}
}

Number 2

import java.util.Scanner;

public class ShoppingApp {

public static void main(String[] args) {


Scanner = new Scanner(System.in);

// Display welcome message and available products


System.out.println("Welcome to the ShoppingApp!");
System.out.println("Here are the available products:");
System.out.println("1. Book - Ugx 1,000");
System.out.println("2. Pen - Ugx 500");
System.out.print("Please enter the number of the product you want to
add: ");

// Get user choice


int productChoice = scanner.nextInt();
String productName = "";
int unitPrice = 0;

// Conditional statements to determine product choice


switch (productChoice) {
case 1:
productName = "Book";
unitPrice = 1000;
System.out.println("You have added a book to your cart.");
break;
case 2:
productName = "Pen";
unitPrice = 500;
System.out.println("You have added a pen to your cart.");
break;
default:
System.out.println("Invalid choice. Please restart the program.");
return;
}

// Prompt for quantity


System.out.print("Please enter the quantity: ");
int quantity = scanner.nextInt();

// Compute total price


int totalPrice = unitPrice * quantity;
System.out.printf("The total price for %d %s(s) is: Ugx %d%n", quantity,
productName, totalPrice);

// Closing the scanner


scanner.close();
}
}

Number 3
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Sacco {


private static final String[] members = {"Sarah", "Hajarah", "Paul",
"Amon"};
private static final int[] amountsSaved = {150000, 400000, 700000,
150000};

public static void main(String[] args) {


SwingUtilities.invokeLater(Sacco::createAndShowGUI);
}

private static void createAndShowGUI() {


JFrame frame = new JFrame("Bico SACCO Member Records");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());

JLabel idLabel = new JLabel("Enter Member ID (1-4):");


JTextField idField = new JTextField(5);
JButton submitButton = new JButton("Submit");
JTextArea outputArea = new JTextArea(10, 30);
outputArea.setEditable(false);

submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
if (id == 0) {
outputArea.setText("Does not exist");
} else if (id >= 1 && id <= 4) {
outputArea.setText(displayMemberInfo(id));
} else {
outputArea.setText("Does not exist");
}
}
});
frame.add(idLabel);
frame.add(idField);
frame.add(submitButton);
frame.add(new JScrollPane(outputArea));

frame.setVisible(true);
}

private static String displayMemberInfo(int id) {


int index = id - 1; // adjust for zero-based index
String memberInfo = "IdNo: " + id + "\nMember: " + members[index] +
"\nAmount Saved: " + amountsSaved[index];
return memberInfo;
}
}

You might also like