• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Troubles with output

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys. Been messing with this for days. I'm not receiving any compile errors, but only generating 0's for output. I'm missing something but can't put a finger on it. Any suggestions on where to begin looking for a solution?

code

public class TimeInterval
{

private int mySecondTime;
private int myFirstTime;

public TimeInterval(int time1, int time2)
{

int myFirstTime = time1;
int mySecondTime = time2;
}

public int calculateHours()
{
int firstMin=myFirstTime/100*60+myFirstTime%100;
int secondMin = mySecondTime/100*60+mySecondTime%100;
int diff = secondMin-firstMin;
return diff/60;
}

int calculateMinutes()
{
int firstMin=myFirstTime/100*60+myFirstTime%100;
int secondMin = mySecondTime/100*60+mySecondTime%100;
int diff = secondMin-firstMin;
return diff%60;
}

public int getFirstTime( )
{
return myFirstTime;
}

public int getSecondTime( )
{
return mySecondTime;
}

public String toString( )
{
String message;
return message = getFirstTime() + " hours " + getSecondTime() + " minutes";

}


}

Test


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class TimeIntervalTest {
public static void main(String[] args) throws IOException
{

BufferedReader console = new BufferedReader( new InputStreamReader(System.in));
System.out.print("Please enter the first time: ");
String input = console.readLine();
int time1 = Integer.parseInt(input); // convert string to int

console = new BufferedReader( new InputStreamReader(System.in));
System.out.print("Please enter the second time: ");
input = console.readLine();
int time2 = Integer.parseInt(input); // convert string to int

TimeInterval interval = new TimeInterval(time1,time2);

interval.calculateHours();
interval.calculateMinutes();

System.out.println (interval.toString());

System.exit(0);
}
}

Thanx for taking a look!
[ October 26, 2004: Message edited by: Tricia Lemay ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem is here

public TimeInterval(int time1, int time2)
{
int myFirstTime = time1;//<----------
int mySecondTime = time2;//<----------
}

by including the data type (int), you make both variables local to the
constructor. Later on, the code accessing the class fields of the same name, the values accessed are 0.

so it becomes

public TimeInterval(int time1, int time2)
{
myFirstTime = time1;
mySecondTime = time2;
}
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is with your constructer. It should be


When you say

You are actually creating two new variables instead of initializing the data members in the class.
 
Tricia Lemay
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's still not calculating right, but most importantly I am getting data! Now, I'll go back and see why I'm not getting what I want.

Thanks for your help.
 
Tricia Lemay
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it working, just needed to change around the calculates and the gets!

Thanx again!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic