English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Design a C# solution with a Main ( ) method that holds an integer variable named seconds to which you will assign a value. Create a method to which you pass this value. The method displays the seconds in minutes and seconds. As an example 66 seconds is 1 minute and 6 seconds.

Add a second method to the solution. This method displays a passed argument as hours, minutes and seconds. For example 3666 seconds is 1 hour, 1 minute and 6 seconds.

Add a statement to the Main ( ) method so that after it calls the methods to convert seconds to minutes and seconds, it passes the same value to the new method to convert it to hours, minutes and seconds

Here is what I wrote and it's not working.
using System;

namespace time
{
public static void Main()
{
// Declare Variables
int sec;
sec = args[0];
displayTime(sec);

{
TimeSpan ts = new TimeSpan(0, 0, x); // where x is the number of seconds
Console.WriteLine( String.Format("{0} minute{1} and {2} second{3}",

2006-11-25 08:50:10 · 3 answers · asked by Christina 2 in Computers & Internet Programming & Design

3 answers

ok, create 3 methods, one called printHours(int time), printMinutes(int time) adn another called printSeconds(int time).

in those methods just print to the screen(time / 24) for hours.
time 60 for seconds adn time devided by 60 for minutes, adn remeber to return any remainders the the calling function.(pass the remainder to the time unit below). thats it.


P.S. welldone for giving it a try rather than just giveing the question and expecting the answers.

2006-11-25 11:26:41 · answer #1 · answered by origamix60 3 · 0 0

Just so I'm not doing ALL your homework for you, I'll do this in VB.NET and see if you're smart enough to convert it to C#.

Function Main(byVal input As Integer)
Dim seconds As Integer = input
PrintMS(seconds)
PrintHMS(seconds)
End Function

Function PrintMS(ByVal secs As Integer) As String
Dim strOut As String, intMins As Integer, intSecs As Integer

'set output string
strOut = "You entered " & secs & " seconds. That is "

'get seconds
intSecs = secs

'get the integer part of the seconds divided by 60
intMins = Fix(intSecs / 60)

'get the remaining seconds
intSecs -= (intMins * 60)

'complete the output string
strOut += intMins & " minutes and " & intSecs & " seconds."

Console.Write(strOut)
Return True
End Function

Function PrintHMS(ByVal secs As Integer)
Dim strOut As String, intHours As Integer, intMins As Integer, intSecs As Integer

'set output string
strOut = "You entered " & secs & " seconds. That is "

'get seconds
intSecs = secs

'get the integer part of the seconds divided by 60 for minutes
intMins = Fix(intSecs / 60)

'get the integer part of the minutes divided by 60 for hours
intHours = Fix(intMins / 60)

'get the remaining seconds
intSecs -= (intMins * 60)

'get the remaining minutes
'intMins -= (intHours * 60)

'complete the output string
strOut += intHours & " hours, " & intMins & " minutes and " & intSecs & " seconds."

Console.Write(strOut)
Return True
End Function


Things to keep in mind:
1. You have to have a variable named seconds in your Main() function.
2. Your two methods will really be functions in the class. You can do those methods as public static.
3. Those functions / methods will return strings, not integers!
4. You could use Timespan, as you are above, but you would want to call the properties Hours, Minutes and Seconds, and you'd still need to do the math I am doing above, so there's no point behind it.

2006-11-25 17:10:32 · answer #2 · answered by Anonymous · 0 0

hi...

nice questin... i worked in it and found a solution

try this one
****************************************************************
using System;

namespace time
{
class test
{
public static void Main(string[] args)
{
int sec;
sec = Convert.ToInt32(args[0]);
displayTime(sec);
}

private static void displayTime(int sec)
{
TimeSpan ts = new TimeSpan(0, 0, sec);
Console.WriteLine(ts.ToString());
}

}
}

********************************************************************

save this code in a file test.cs

compile it in a command window using csc test.cs

and run like below..

c:/>csc test.cs

c:/>test 3600

01:01:00


hope u got ur answer.....

keep asking questions.......


regards.. raja

2006-11-25 17:12:28 · answer #3 · answered by Solairaja A 2 · 0 0

fedest.com, questions and answers