0% found this document useful (0 votes)
33 views

Patel Arth Cosclab1

This document contains a student's submission for Lab 1 of an introduction to computer science course. The student created a Timer class with fields for hours, minutes, and seconds along with getter and setter methods. The main method then prompts the user to input times for three timers as hours, minutes, seconds and stores them in an array of Timer objects. It calculates the total time across the three timers and prints the result. In conclusion, the student created a Timer class to hold time values and a program that takes user input for multiple timers and calculates the total time.

Uploaded by

Arth Patel
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)
33 views

Patel Arth Cosclab1

This document contains a student's submission for Lab 1 of an introduction to computer science course. The student created a Timer class with fields for hours, minutes, and seconds along with getter and setter methods. The main method then prompts the user to input times for three timers as hours, minutes, seconds and stores them in an array of Timer objects. It calculates the total time across the three timers and prints the result. In conclusion, the student created a Timer class to hold time values and a program that takes user input for multiple timers and calculates the total time.

Uploaded by

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

Lab 1

Course: COSC 1047 Introduction to Computer Science II


Name: Arth Patel
Section: COSC 1047 J
Student number: 239548040
Q1.

Answer:
import java.util.Scanner;

public class Timer


{
private int hours;
private int minutes;
private int seconds;

public Timer()
{
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}

public Timer(int hours, int minutes, int seconds)


{
set_Hours(hours);
set_Minutes(minutes);
set_Seconds(seconds);
}
public int get_Seconds()
{
return seconds;
}
public void set_Seconds(int seconds)
{
if (seconds < 0)
{
this.seconds = 0;
}
else
{
this.seconds = seconds;
}
}
public int get_Minutes()
{
return minutes;
}
public void set_Minutes(int minutes)
{
if (minutes < 0)
{
this.minutes = 0;
}
else
{
this.minutes = minutes;
}
}
public int get_Hours()
{
return hours;
}
public void set_Hours(int hours)
{
if (hours < 0)
{
this.hours = 0;
}
else
{
this.hours = hours;
}
}

public static void main(String[] args)


{
Scanner scanner = new Scanner(System.in);// create scanner class for take
the inputs from the user.
Timer[] t = new Timer[3];

for (int i = 0; i < t.length; i++)


{
System.out.print(" enter Timer Object" + (i + 1) + " : ");
int hr = scanner.nextInt();
int min = scanner.nextInt();
int sec = scanner.nextInt();
t[i] = new Timer(hr, min, sec);
}

scanner.close();

int total_Hrs = 0;
int total_Mins = 0;
int total_Secs = 0;

for (Timer timer : t)


{
total_Hrs += timer.get_Hours();
total_Mins += timer.get_Minutes();
total_Secs += timer.get_Seconds();
}
total_Mins += total_Secs / 60;
total_Secs %= 60;
total_Hrs += total_Mins / 60;
total_Mins %= 60;
System.out.println("total hours : " + total_Hrs);
System.out.println("total minutes : " + total_Mins);
System.out.println("total seconds : " + total_Secs);
}
}

Conclusion and summery of work:


In the mentioned program I create a Timer class that contains three instance data
fields (Hours, Minutes, and Seconds). It also contains constructor, setter methods
and getter methods. After that we take the data from user and after that creates an
array of Timer object, and that calculates the total Hours, minutes and seconds. In
the last I put print statement that print the results.

You might also like