hi i am working on a temperautre convertor and it has to have 2 methods and a return value for each method but i dont know how to do it this is what i have so far
import java.util.Scanner;
class Temperature
{
public static void main(String[] args)
{
int Temp1,cel;
System.out.print("What is the temperature you'd like to convert? ");
Scanner myScanner = new Scanner(System.in);
Temp1 = myScanner.nextInt();
cel = (Temp1 / 5) * 32;
System.out.println("Celsius to Fahrenheit = " + cel);
}
}
adding methods
Started by justinb09, May 08 2009 09:24 AM
8 replies to this topic
#1
Posted 08 May 2009 - 09:24 AM
|
|
|
#2
Posted 08 May 2009 - 12:04 PM
If you want it oop just create another class that has 2 methods - define an object of the class and use the 2 methods to calculate the totals - it looks like you know the formulas for it.
class BlaineSch {
celcius(int i) {
return (i/5)*32
}
ferenheit(int i) {
return (i/32)*5;
}
}
class Temperature {
public static void main(String[] args) {
int Temp1,cel;
System.out.print("If you would like celcius to ferenheiht type in the number one");
System.out.print("If you would like ferenheiht to celcius type in the number two");
Scanner myScanner = new Scanner(System.in);
Temp1 = myScanner.nextInt();
BlaineSch Blaine = new BlaineSch();
if(Temp1==1) {
blaine.celcius(Temp2);
} else {
blaine.ferenheit(Temp2);
}
}
}
I just typed that up in the quick reply box so it probably has enough errors you have to fix in order to actually understand what its doing.
#3
Posted 08 May 2009 - 12:12 PM
Looks promising, let me help the new commoner !
So try to figure out what I have done and check here !(Do it for my and yourself please !)
Scanner (Java 2 Platform SE 5.0)
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Modifier (Java 2 Platform SE 5.0)
Read them whenever your stuck and need to remind where the walls are around the box; which means before you leave outside the box alone :-)
Cheers !!
Also rep+ me please :x
justinb09 said:
hi i am working on a temperautre convertor and it has to have 2 methods and a return value for each method but i dont know how to do it this is what i have so far
import java.util.Scanner;
class Temperature
{
public static void main(String[] args)
{
int Temp1,cel;
System.out.print("What is the temperature you'd like to convert? ");
Scanner myScanner = new Scanner(System.in);
Temp1 = myScanner.nextInt();
cel = (Temp1 / 5) * 32;
System.out.println("Celsius to Fahrenheit = " + cel);
}
}
import java.util.Scanner;
class Temperature
{
public static void main(String[] args)
{
int Temp1,cel;
System.out.print("What is the temperature you'd like to convert? ");
Scanner myScanner = new Scanner(System.in);
Temp1 = myScanner.nextInt();
cel = (Temp1 / 5) * 32;
System.out.println("Celsius to Fahrenheit = " + cel);
}
}
//Our package we need to use the scanner...
import java.util.*;
public class FaCel {
/*Our scanner, by adding static we can use it
anywhere we want without any hassle(even in a method...)*/
static Scanner sc = new Scanner(System.in);
public static void main(String[] arg) {
/*Making it loop back whenever we are done with our operation allowing the user
To continue back...*/
while(true) {
System.out.println("What do you want to do?"+"\n"+
"1.Fahrenheit to celcius"+"\n"+
"2.Celcius to fahrenheit"+"\n"+
"3.Close the application...");
//The one that will catch our value of options !
int operation = sc.nextInt();
/*This part will check if our input was one of the criteria we wanted it to be
* */
if(operation == 1) {
//Fahrenheit to Celcius !
System.out.println("Whats the temperature? ");
//Reads in the value
float fTc = sc.nextFloat();
//Teh converter, simple math...
float Ctemp = (float) ((fTc-32)/1.8);
//AND here is the output in celcius wow !
System.out.println("Celcius: "+Ctemp);
}
else if(operation == 2) {
System.out.println("Whats the temperature? ");
float cTf = sc.nextFloat();
float Ftemp = (float) (cTf*1.8+32);
System.out.println("Fahrenheit: "+Ftemp);
}
else if(operation == 3) {
System.out.println("Bye !");
break;
}
}
}
}
So, I have done one for you just this time, since I want to be happy and kind, and lol.So try to figure out what I have done and check here !(Do it for my and yourself please !)
Scanner (Java 2 Platform SE 5.0)
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Modifier (Java 2 Platform SE 5.0)
Read them whenever your stuck and need to remind where the walls are around the box; which means before you leave outside the box alone :-)
Cheers !!
Also rep+ me please :x
#4
Posted 08 May 2009 - 12:14 PM
#5
Posted 08 May 2009 - 02:01 PM
but neither one of these have return parimiters it needs to have them
#6
Posted 08 May 2009 - 11:33 PM
justinb09 said:
but neither one of these have return parimiters it needs to have them
Uhm, Blaine have a return, statement in each of his converts, you can't have return inside of parameters...(not today at least)
#7
Posted 09 May 2009 - 01:57 PM
Beat ya to it didnt I? :P
Either way yours looks tested lol mine probably has more errors then a 3 year old doing a "hello world" program :P
Either way yours looks tested lol mine probably has more errors then a 3 year old doing a "hello world" program :P
#8
Posted 10 May 2009 - 10:03 AM
ok do a step by step explanation on how to make my code into 2 methods and have two return values in it and convert from celsius to farenheit and vice versa please use my above code to explain this please show me each step of code
#9
Posted 10 May 2009 - 12:57 PM
BlaineSch said:
class BlaineSch {
celcius(int i) {
return (i/5)*32
}
ferenheit(int i) {
return (i/32)*5;
}
}
class Temperature {
public static void main(String[] args) {
int Temp1,cel;
System.out.print("If you would like celcius to ferenheiht type in the number one");
System.out.print("If you would like ferenheiht to celcius type in the number two");
Scanner myScanner = new Scanner(System.in);
Temp1 = myScanner.nextInt();
BlaineSch Blaine = new BlaineSch();
if(Temp1==1) {
blaine.celcius(Temp2);
} else {
blaine.ferenheit(Temp2);
}
}
}
Well lets see...
//Subclass...
class BlaineSch {
//Celcius converter... with parameter int i, if you put in something...
celcius(int i) {
//it converts here and returns the value....
return (i/5)*32
}
//Fahrenheit converter... with parameter int i, if you put in something...
ferenheit(int i) {
//it converts here and returns the value...
return (i/32)*5;
}
}
class Temperature {
public static void main(String[] args) {
int Temp1,cel;
System.out.print("If you would like celcius to ferenheiht type in the number one");
System.out.print("If you would like ferenheiht to celcius type in the number two");
Scanner myScanner = new Scanner(System.in);
Temp1 = myScanner.nextInt();
//reference to the stupid subclass...
BlaineSch Blaine = new BlaineSch();
checks your fukking option from Temp1...
if(Temp1==1) {
//Counts in the value refers to the subclass method...and returns value...
blaine.celcius(Temp2);
} else {
//Counts in the value refers to the subclass method...and returns value...
blaine.ferenheit(Temp2);
}
}
}
The end


Sign In
Create Account


Back to top










