Welcome to Dream.In.Code
Become a Java Expert!

Join 149,520 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,399 people online right now. Registration is fast and FREE... Join Now!




Frustration with methods

 
Reply to this topicStart new topic

Frustration with methods, Client Class

jreibe3000
26 Jun, 2007 - 03:09 PM
Post #1

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
Hello everyone!

I have written a program that uses two service classes and one client class to call
them. The goal is to create 5 small reports based on some user input and calculations.
Two of the classes compile just fine, but the client class, entitled L6EarthTemperatureClient,
is generating one single compiler error that I can't figure out.
The error is // <identifier> expected
and it is on the line // MyInfo.topOfPageInfo();

which is a call to the method topOfPageInfo() of the MyInfo class. The method is static, so I don't think an object needs to be instantiated before the call.I will post this client class first, then the other two, in case they are needed to fix the problem as well.

Thanks a ton for any help!
Jake

CODE

public class L6EarthTemperatureClient
{
   L6EarthTemperatureService object = new L6EarthTemperatureService();
    DecimalFormat temps = new DecimalFormat("0.0");
        
    final String DASHES = "------------------------------------------------------";
    final String SHORT_DASHES = "----------------";
    
    int count = 0;
    
    MyInfo.topOfPageInfo();  //HERE LIES THE ERROR!!
    
    public static void main(String[] arg)
    {
       while(count<5)
        {
           count++;
            
            System.out.println("Reading No. " + count);
            System.out.println(SHORT_DASHES);
            object.readDepth();
            System.out.println("Depth In Kilometers = " + object.temps.format(getDepthInKilometers()));
            System.out.println("Celsius Temp At This Depth = " + object.temps.format(getCelsius()));
            System.out.println("Equivalent Fahrenheit Temp = " + object.temps.format(getFahrenheit()));
            System.out.println(DASHES);
        }
    }
}

import java.util.*;

public class L6EarthTemperatureService
{
   private double depthInMiles;
    private double depthInKilometers;
    private double celsius;
    private double fahrenheit;
    
    public L6EarthTemperatureService()
    {
    }
    
    public double getDepthInMiles()
    {
       return depthInMiles;
    }
    
    public double getDepthInKilometers()
    {
       return depthInKilometers;
    }
    
    public double getCelsius()
    {
       return celsius;
    }
    
    public double getFahrenheit()
    {
       return fahrenheit;
    }
    
    public void setDepthInMiles(double newDepthInMiles)
    {
       depthInMiles = newDepthInMiles;
    }
    
    public void setDepthInKilometers(double newDepthInKilometers)
    {
       depthInKilometers = newDepthInKilometers;
    }
    
    public void setCelsius(double newCelsius)
    {
       celsius = newCelsius;
    }
    
    public void setFahrenheit(double newFahrenheit)
    {
       fahrenheit = newFahrenheit;
    }
    
    public void readDepth()
    {
       Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter Depth In Miles: ");
        depthInMiles = scan.nextDouble();
    }
    
    public void convertToKilometers()
    {
       final double MILES_TO_KILOMETERS = 1.6;
        
        depthInKilometers = MILES_TO_KILOMETERS*depthInMiles;
    }
    
    public void computeCelsius()
    {
       celsius = 18*depthInKilometers+20;
    }
    
    public void convertToFahrenheit()
    {
       fahrenheit = 1.8*celsius+32;
    }
}


import java.util.*;

public class MyInfo
{
   static void topOfPageInfo()
    {
       final String MY_NAME = "Jake Reibert";
       final String DASHES = "---------------------------------------------------";    

        System.out.println(DASHES + "\n\t\t\tName: " + MY_NAME);
        System.out.println("\t\t\tProject#6");
        System.out.println("\t\t\tDue Date:  06/26/2007");
        
        GregorianCalendar today = new GregorianCalendar();
        
        System.out.println("\n\t\t\tToday's Date:   "
                                  + (today.get(Calendar.MONTH)+1)
                                     + '/' +        today.get(Calendar.DAY_OF_MONTH)
                                     + '/' +        today.get(Calendar.YEAR));
        System.out.println("\t\t\tThe Time is Now: " + (today.get(Calendar.HOUR))
                                  + ':' + (today.get(Calendar.MINUTE))
                                     + ':' + (today.get(Calendar.SECOND)));
        System.out.println ("\t" + DASHES + "\n");
    }
}


User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Frustration With Methods
26 Jun, 2007 - 05:32 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
This error is simple, the problem is that this call to a method is not within any method.. eg it is within the global declarations. Move it into a constructor or another method to solve the problem.
User is offlineProfile CardPM
+Quote Post

jreibe3000
RE: Frustration With Methods
26 Jun, 2007 - 07:08 PM
Post #3

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
Well Hello!

I can't figure out why I'm still gettin these six error messages when I compile my client class, even
after I went through it a couple times. I'll post the errors first, then my client class, then my 2 service class below that just in case looking at them might help someone see what's going on.

Thanks,
Jake

The errors are:
L6EarthTemperatureClient.java:34: cannot find symbol
symbol : method getDepthInKilometers()
location: class L6EarthTemperatureClient
System.out.println("Depth In Kilometers = " + object.temps.format(getDepthInKilometers()));
^
L6EarthTemperatureClient.java:34: cannot find symbol
symbol : variable temps
location: class L6EarthTemperatureService
System.out.println("Depth In Kilometers = " + object.temps.format(getDepthInKilometers()));
^
L6EarthTemperatureClient.java:35: cannot find symbol
symbol : method getCelsius()
location: class L6EarthTemperatureClient
System.out.println("Celsius Temp At This Depth = " + object.temps.format(getCelsius()));
^
L6EarthTemperatureClient.java:35: cannot find symbol
symbol : variable temps
location: class L6EarthTemperatureService
System.out.println("Celsius Temp At This Depth = " + object.temps.format(getCelsius()));
^
L6EarthTemperatureClient.java:36: cannot find symbol
symbol : method getFahrenheit()
location: class L6EarthTemperatureClient
System.out.println("Equivalent Fahrenheit Temp = " + object.temps.format(getFahrenheit()));
^
L6EarthTemperatureClient.java:36: cannot find symbol
symbol : variable temps
location: class L6EarthTemperatureService
System.out.println("Equivalent Fahrenheit Temp = " + object.temps.format(getFahrenheit()));
^
6 errors

// below first is my client class where I'm getting the errors.

CODE

import java.text.*;

public class L6EarthTemperatureClient
{
        
    static final String DASHES = "------------------------------------------------------";
    static final String SHORT_DASHES = "----------------";
    
    static int count = 0;
    
    public static void main(String[] arg)
    {
      MyInfo.topOfPageInfo();
      L6EarthTemperatureService object = new L6EarthTemperatureService();
      DecimalFormat temps = new DecimalFormat("0.0");

       while(count<5)
        {    
           count++;
            
            System.out.println("Reading No. " + count);
            System.out.println(SHORT_DASHES);
            object.readDepth();
            System.out.println("Depth In Kilometers = " + object.temps.format(getDepthInKilometers()));
            System.out.println("Celsius Temp At This Depth = " + object.temps.format(getCelsius()));
            System.out.println("Equivalent Fahrenheit Temp = " + object.temps.format(getFahrenheit()));
            System.out.println(DASHES);
        }
    }
}

import java.util.*;

public class L6EarthTemperatureService
{
   private double depthInMiles;
    private double depthInKilometers;
    private double celsius;
    private double fahrenheit;
    
    public L6EarthTemperatureService()
    {
    }
    
    public double getDepthInMiles()
    {
       return depthInMiles;
    }
    
    public double getDepthInKilometers()
    {
       return depthInKilometers;
    }
    
    public double getCelsius()
    {
       return celsius;
    }
    
    public double getFahrenheit()
    {
       return fahrenheit;
    }
    
    public void setDepthInMiles(double newDepthInMiles)
    {
       depthInMiles = newDepthInMiles;
    }
    
    public void setDepthInKilometers(double newDepthInKilometers)
    {
       depthInKilometers = newDepthInKilometers;
    }
    
    public void setCelsius(double newCelsius)
    {
       celsius = newCelsius;
    }
    
    public void setFahrenheit(double newFahrenheit)
    {
       fahrenheit = newFahrenheit;
    }
    
    public void readDepth()
    {
       Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter Depth In Miles: ");
        depthInMiles = scan.nextDouble();
    }
    
    public void convertToKilometers()
    {
       final double MILES_TO_KILOMETERS = 1.6;
        
        depthInKilometers = MILES_TO_KILOMETERS*depthInMiles;
    }
    
    public void computeCelsius()
    {
       celsius = 18*depthInKilometers+20;
    }
    
    public void convertToFahrenheit()
    {
       fahrenheit = 1.8*celsius+32;
    }
}
    
    
import java.util.*;

public class MyInfo
{
   static void topOfPageInfo()
    {
       final String MY_NAME = "Jake Reibert";
       final String DASHES = "---------------------------------------------------";    

        System.out.println(DASHES + "\n\t\t\tName: " + MY_NAME);
        System.out.println("\t\t\tProject#6");
        System.out.println("\t\t\tDue Date:  06/26/2007");
        
        GregorianCalendar today = new GregorianCalendar();
        
        System.out.println("\n\t\t\tToday's Date:   "
                                  + (today.get(Calendar.MONTH)+1)
                                     + '/' +        today.get(Calendar.DAY_OF_MONTH)
                                     + '/' +        today.get(Calendar.YEAR));
        System.out.println("\t\t\tThe Time is Now: " + (today.get(Calendar.HOUR))
                                  + ':' + (today.get(Calendar.MINUTE))
                                     + ':' + (today.get(Calendar.SECOND)));
        System.out.println ("\t" + DASHES + "\n");
    }
}

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Frustration With Methods
26 Jun, 2007 - 07:24 PM
Post #4

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
please do not make a new thread for the same code and the same set of errors.


all of your errors are due to the same error in the . operator:
CODE

object.temps.format(getDepthInKilometers())

should be:
CODE

temps.format(object.getDepthInKilometers())


User is offlineProfile CardPM
+Quote Post

jreibe3000
RE: Frustration With Methods
26 Jun, 2007 - 07:51 PM
Post #5

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
QUOTE(William_Wilson @ 26 Jun, 2007 - 08:24 PM) *

please do not make a new thread for the same code and the same set of errors.


all of your errors are due to the same error in the . operator:
CODE

object.temps.format(getDepthInKilometers())

should be:
CODE

temps.format(object.getDepthInKilometers())



Thank you! Now I have another problem. My output is not coming out the way it's supposed to.
The numbers I'm supposed to run the program on are 6.3, 100.0, 8.5, 1.0, and 555.5.
I'm getting zeros where I should be getting different values.

//The output Im getting is below
---------------------------------------------------
Name: Jake Reibert
Project#6
Due Date: 06/26/2007

Today's Date: 6/26/2007
The Time is Now: 8:43:14
---------------------------------------------------

Reading No. 1
----------------
Enter Depth In Miles: 6.3
Depth In Kilometers = 0.0
Celsius Temp At This Depth = 0.0
Equivalent Fahrenheit Temp = 0.0
------------------------------------------------------
Reading No. 2
----------------
Enter Depth In Miles: 100.0
Depth In Kilometers = 0.0
Celsius Temp At This Depth = 0.0
Equivalent Fahrenheit Temp = 0.0
------------------------------------------------------
Reading No. 3
----------------
Enter Depth In Miles: 8.5
Depth In Kilometers = 0.0
Celsius Temp At This Depth = 0.0
Equivalent Fahrenheit Temp = 0.0
------------------------------------------------------
Reading No. 4
----------------
Enter Depth In Miles: 1.0
Depth In Kilometers = 0.0
Celsius Temp At This Depth = 0.0
Equivalent Fahrenheit Temp = 0.0
------------------------------------------------------
Reading No. 5
----------------
Enter Depth In Miles: 555.5
Depth In Kilometers = 0.0
Celsius Temp At This Depth = 0.0
Equivalent Fahrenheit Temp = 0.0
------------------------------------------------------

----jGRASP: operation complete.

//WHY AM I GETTING ZEROS FOR ALL THESE THINGS???








User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Frustration With Methods
27 Jun, 2007 - 02:52 AM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi,
QUOTE
System.out.println("Depth In Kilometers = " + object.temps.format(getDepthInKilometers());
System.out.println("Celsius Temp At This Depth = " + object.temps.format(getCelsius());
System.out.println("Equivalent Fahrenheit Temp = " + object.temps.format(getFahrenheit());


shouldn't you call here in the main instead of getDepthInKilometers() =>
convertToKilometers()

and instead of getCelsius() =>
computeCelsius()

and instead of getFahrenheit() =>
convertToFahrenheit()

This post has been edited by PennyBoki: 27 Jun, 2007 - 02:53 AM
User is offlineProfile CardPM
+Quote Post

jreibe3000
RE: Frustration With Methods
27 Jun, 2007 - 05:46 AM
Post #7

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
QUOTE(PennyBoki @ 27 Jun, 2007 - 03:52 AM) *

Hi,
QUOTE
System.out.println("Depth In Kilometers = " + object.temps.format(getDepthInKilometers());
System.out.println("Celsius Temp At This Depth = " + object.temps.format(getCelsius());
System.out.println("Equivalent Fahrenheit Temp = " + object.temps.format(getFahrenheit());


shouldn't you call here in the main instead of getDepthInKilometers() =>
convertToKilometers()

and instead of getCelsius() =>
computeCelsius()

and instead of getFahrenheit() =>
convertToFahrenheit()



Thank you very much, your absolutely right.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:22PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month