0% found this document useful (0 votes)
39 views4 pages

Cs506 Assignment No 1 2024

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)
39 views4 pages

Cs506 Assignment No 1 2024

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/ 4

ASSIGNMENT NO 1

CS506.

import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Marathon {

public static void main(String[] args) {

new Marathon().init();

private void init() {

try {

int numParticipants = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of


participants:"));

if (numParticipants <= 0) {

JOptionPane.showMessageDialog(null, "Number of participants must be greater than zero.");

return;

ArrayList<Runner> runners = new ArrayList<>();

for (int i = 1; i <= numParticipants; i++) {

String name = JOptionPane.showInputDialog("Enter the name of runner " + i + ":");

if (name == null || name.trim().isEmpty()) {

JOptionPane.showMessageDialog(null, "Runner name cannot be empty.");


return;

int time = Integer.parseInt(JOptionPane.showInputDialog("Enter the finishing time of runner " +


i + " in minutes:"));

if (time <= 0) {

JOptionPane.showMessageDialog(null, "Finishing time must be a positive number.");

return;

runners.add(new Runner(name, time));

Runner fastestRunner = findFastestRunner(runners);

JOptionPane.showMessageDialog(null, "Fastest runner is " + fastestRunner.getName() + " with


finishing time " + fastestRunner.getTime() + " minutes.");

JOptionPane.showMessageDialog(null, "VU Student ID: mc22020000");

} catch (NumberFormatException e) {

JOptionPane.showMessageDialog(null, "Invalid input. Please enter a valid number.");

public Runner findFastestRunner(ArrayList<Runner> runners) {

Runner fastestRunner = runners.get(0);

for (int i = 1; i < runners.size(); i++) {


if (runners.get(i).getTime() < fastestRunner.getTime()) {

fastestRunner = runners.get(i);

return fastestRunner;

private class Runner {

private String name;

private int time;

public Runner() {

public Runner(String name, int time) {

this.name = name;

this.time = time;

public Runner(Runner runner) {

this.name = runner.name;

this.time = runner.time;

public String getName() {


return name;

public void setName(String name)

this.name = name;

public int getTime() {

return time;

public void setTime(int time) {

this.time = time;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy