Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to make my code run but it keeps giving me an error...// using Syste

ID: 441892 • Letter: I

Question

I'm trying to make my code run but it keeps giving me an error...// using System; using System.Collections.Generic; using System.Linq; using System.Text; public class Time2 { private int totalSeconds; public Time2() { totalSeconds = 0; } public Time2( int h ) { setTime( h, 0, 0 ); } public Time2( int h, int m ) { setTime( h, m, 0 ); } public Time2( int h, int m, int s ) { setTime( h, m, s ); } public Time2( Time2 time ) { setTime( time.getHour(), time.getMinute(), time.getSecond() ); } public void setTime( int h, int m, int s ) { setHour( h ); setMinute( m ); setSecond( s ); } public void setHour( int h ) { int hours = ( h >= 0 && h < 24 ) ? h : 0; totalSeconds = ( hours * 3600 ) + ( getMinute() * 60 ) + getSecond(); } public void setMinute( int m ) { int minutes = ( m >= 0 && m < 60 ) ? m : 0; totalSeconds = ( getHour() * 3600 ) + ( minutes * 60 ) + getSecond(); } public void setSecond( int s ) { int seconds = ( s >= 0 && s < 60 ) ? s : 0; totalSeconds = ( getHour() * 3600 ) + ( getMinute() *60 ) + seconds; } public int getHour() { return ( totalSeconds / 3600 ); } public int getMinute() { return ( ( totalSeconds % 3600 ) / 60 ); } public int getSecond() { return ( ( totalSeconds % 3600 ) % 60 ); } public String toUniversalString() { Console.WriteLine( "{0}:{1}:{2}", getHour(), getMinute(), getSecond() ); } public String toString() { Console.WriteLine( "{0}:{0}:{0}", ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ), getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) ); } } public class Time2Test { public static void main( String[] args ) { Time2 t = new Time2(); Console.WriteLine( "The initial universal time is: " ); Console.WriteLine( t.toUniversalString() ); Console.WriteLine( "The initial standard time is: " ); Console.WriteLine( t.toString() ); t.setTime( 13, 27, 6 ); Console.WriteLine( " Universal time after setTime is: " ); Console.WriteLine( t.toUniversalString() ); Console.WriteLine( "Standard time after setTime is: " ); Console.WriteLine( t.toString() ); t.setTime( 99, 99, 99 ); Console.WriteLine( " After attempting invalid settings:" ); Console.WriteLine( "The initial universal time is: " ); Console.WriteLine( t.toUniversalString() ); Console.WriteLine( "The initial standard time is: " ); Console.WriteLine( t.toString() ); } }

Explanation / Answer

// 2 function return value is string. but actually not returning anything..


// i made them as void.


/// now run it.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Time2
{
private int totalSeconds;
public Time2()
{
totalSeconds = 0;

}
public Time2( int h )
{
setTime( h, 0, 0 );

} public Time2( int h, int m )
{
setTime( h, m, 0 );
}
public Time2( int h, int m, int s )
{
setTime( h, m, s );

}
public Time2( Time2 time )
{
setTime( time.getHour(), time.getMinute(), time.getSecond() );

}
public void setTime( int h, int m, int s )
{
setHour( h );
setMinute( m );
setSecond( s );
}
public void setHour( int h )
{
int hours = ( h >= 0 && h < 24 ) ? h : 0;
totalSeconds = ( hours * 3600 ) + ( getMinute() * 60 ) + getSecond();
}
public void setMinute( int m )
{
int minutes = ( m >= 0 && m < 60 ) ? m : 0;
totalSeconds = ( getHour() * 3600 ) + ( minutes * 60 ) + getSecond();
}
public void setSecond( int s )
{
int seconds = ( s >= 0 && s < 60 ) ? s : 0;
totalSeconds = ( getHour() * 3600 ) + ( getMinute() *60 ) + seconds;
}
public int getHour()
{ return ( totalSeconds / 3600 );
}
public int getMinute()
{
return ( ( totalSeconds % 3600 ) / 60 );

}
public int getSecond()
{
return ( ( totalSeconds % 3600 ) % 60 );
}
public void toUniversalString()
{
Console.WriteLine( "{0}:{1}:{2}", getHour(), getMinute(), getSecond() );
}
public void toString()
{
Console.WriteLine( "{0}:{0}:{0}", ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ), getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );

}

}
public class Time2Test
{
public static void main( String[] args )
{
Time2 t = new Time2();
Console.WriteLine( "The initial universal time is: " );
Console.WriteLine( t.toUniversalString() );
Console.WriteLine( "The initial standard time is: " );
Console.WriteLine( t.toString() ); t.setTime( 13, 27, 6 );
Console.WriteLine( " Universal time after setTime is: " );
Console.WriteLine( t.toUniversalString() );
Console.WriteLine( "Standard time after setTime is: " );
Console.WriteLine( t.toString() ); t.setTime( 99, 99, 99 );
Console.WriteLine( " After attempting invalid settings:" );
Console.WriteLine( "The initial universal time is: " );
Console.WriteLine( t.toUniversalString() );
Console.WriteLine( "The initial standard time is: " );
Console.WriteLine( t.toString() );
}

}