Re: Java class problem

  • From: Alex Hall <mehgcap@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sun, 27 Jun 2010 21:54:11 -0400

No problem. Just remember to now do something like
result=cel2fahr(15);
System.out.println(result);
or the method will return but you will never see the result since the
cel2fahr method no longer prints.

On 6/27/10, Gilbert Neiva <gneiva@xxxxxxx> wrote:
> Thanks. I implamented your suggestion and fixed the method by doing the
> following:
>
> return "Close enough";
>
> and
>
> return "Will not do";
>
> The program works now.
>
> Gilbert Neiva
>
> ----- Original Message -----
> From: "Alex Hall" <mehgcap@xxxxxxxxx>
> To: <programmingblind@xxxxxxxxxxxxx>
> Sent: Sunday, June 27, 2010 5:04 PM
> Subject: Re: Java class problem
>
>
>> Seems to me that the problem is when you try to return the print
>> statement. Print is a function call and has no return; when you call
>> system.out.print, you expect only printed text, and nothing is
>> returned. Therefore, when you try to return the value of a print, you
>> are essentiall saying "return void", which will not do since your
>> class returns a string. Either make your class return void, or just
>> return the "close enough" or "will not do" strings and print them with
>> the function that called them.
>>
>> On 6/27/10, Gilbert Neiva <gneiva@xxxxxxx> wrote:
>>> I'm trying to make a class that has a method to convert a celsius to two
>>> values, approximate fahrenheit, and accurate fahrenheit. The method is
>>> below.
>>>
>>> public static String celsFahr()
>>>
>>> // This method will print "Close enough"
>>>
>>> // If the values of approximate fahrenheit
>>>
>>> // are within two degrees of each other and
>>>
>>> // it will print "Will not do" if those two
>>>
>>> // values are not within
>>>
>>> // two degrees of each other
>>>
>>> {
>>>
>>> appFahr = cels * 2 + 32;
>>>
>>> // Formula to convert
>>>
>>> // celsius to approximate fahrenheit
>>>
>>> fahr = (9 / 5) * cels + 32;
>>>
>>> // Formula to convert celsius to accurate fahrenheit
>>>
>>> lessFahr = fahr - 3;
>>>
>>> greatFahr = fahr + 3;
>>>
>>>
>>> if ((appFahr <= fahr) && (appFahr > lessFahr) || (appFahr >= fahr) &&
>>> (appFahr < greatFahr))
>>>
>>> {
>>>
>>> return System.out.println("Close enough");
>>>
>>> }
>>>
>>> else
>>>
>>> {
>>>
>>> return System.out.println("Will not do");
>>>
>>> }
>>>
>>> } // End of method
>>>
>>>
>>>
>>> When I try to compile the .java file, I get the following error.
>>>
>>>
>>>
>>> FahrClss.java:90: incompatible types
>>> found   : void
>>> required: java.lang.String
>>> return System.out.println("Close enough");
>>> ^
>>> FahrClss.java:94: incompatible types
>>> found   : void
>>> required: java.lang.String
>>> return System.out.println("Will not do");
>>> ^
>>> 2 errors
>>>
>>>
>>>
>>> I tried importing java.lang.String at the top of my class.
>>>
>>>
>>>
>>> import java.lang.String;
>>>
>>>
>>>
>>> Unfortunately, that did not work. What am I doing wrong. My entire class
>>> is
>>> below.
>>>
>>>
>>>
>>> public class FahrClss
>>>
>>> {
>>>
>>> static int cels; // Stores the value for celsius
>>>
>>> static int appFahr; // Stores the value
>>>
>>> // of approximate fahrenheit
>>>
>>> static int fahr; // Stores the value of
>>>
>>> // exact fahrenheit
>>>
>>> static int lessFahr; // Stores the value of
>>>
>>> // fahr - 3
>>>
>>> static int greatFahr; // Stores the value of
>>>
>>> // fahr + 3
>>>
>>>
>>>
>>>
>>> public FahrClss(int newCels)
>>>
>>> // Start of parameterized constructor
>>>
>>> {
>>>
>>> cels = newCels; // Stores the value passed
>>>
>>> // from newCels
>>>
>>> }
>>>
>>>
>>>
>>>
>>> public FahrClss()
>>>
>>> // Start of default constructor
>>>
>>> {
>>>
>>> cels = 0; // object gets set to 0
>>>
>>> }
>>>
>>>
>>>
>>>
>>> public static int getCels()
>>>
>>> // Start of observer method
>>>
>>> {
>>>
>>> return cels; // Returns the value of
>>>
>>> // cels to driver class
>>>
>>> }
>>>
>>>
>>>
>>>
>>> public static int getAppFahr()
>>>
>>> // Start of observer method
>>>
>>> {
>>>
>>> return cels * 2 + 32; // Returns the value of
>>>
>>> // approximate fahrenheit to driver class
>>>
>>> }
>>>
>>>
>>>
>>>
>>> public static int getFahr()
>>>
>>> // Start of observer method
>>>
>>> {
>>>
>>> return (9 / 5) * cels + 32; // Returns the value of
>>>
>>> // fahrenheit to driver class
>>>
>>> }
>>>
>>>
>>>
>>>
>>> public static String celsFahr()
>>>
>>> // This method will print "Close enough"
>>>
>>> // If the values of approximate fahrenheit
>>>
>>> // are within two degrees of each other and
>>>
>>> // it will print "Will not do" if those two
>>>
>>> // values are not within
>>>
>>> // two degrees of each other
>>>
>>> {
>>>
>>> appFahr = cels * 2 + 32;
>>>
>>> // Formula to convert
>>>
>>> // celsius to approximate fahrenheit
>>>
>>> fahr = (9 / 5) * cels + 32;
>>>
>>> // Formula to convert celsius to accurate fahrenheit
>>>
>>> lessFahr = fahr - 3;
>>>
>>> greatFahr = fahr + 3;
>>>
>>>
>>>
>>>
>>> if ((appFahr <= fahr) && (appFahr > lessFahr) || (appFahr >= fahr) &&
>>> (appFahr < greatFahr))
>>>
>>> {
>>>
>>> return System.out.println("Close enough");
>>>
>>> }
>>>
>>> else
>>>
>>> {
>>>
>>> return System.out.println("Will not do");
>>>
>>> }
>>>
>>> } // End of method
>>>
>>> } // End of class
>>>
>>>
>>>
>>> Gilbert Neiva
>>>
>>>
>>>
>>
>>
>> --
>> Have a great day,
>> Alex (msg sent from GMail website)
>> mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap
>> __________
>> View the list's information and change your settings at
>> //www.freelists.org/list/programmingblind
>>
>
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: