So I am supposed to write a class that converts fahrenheit to celsius (& vice versa), calculates the volume of a sphere after a radius is entered, and calculates the hypotenuse of a right triangle, all using nothing but static methods. Here is what I have so far;

class Fun {
	
	static double myFahrenheit = fahrenheit;
	static double myCelsius = celsius;
	static double myVolume = volume;
	static double myRadius = radius;
	static double myA = a;
	static double myB = b;
	static double myC = c;
	
	static void getFahrenheit(double celsius) {
		
		myFahrenheit = ((9.0 / 5.0) * myCelsius) + 32;
		
	}
	
	static void getCelsius(double fahrenheit) {
		
		myCelsius = (5.0 / 9.0) * (myFahrenheit - 32;)
		
	}
			
	static void getVolume(double radius) {
		
		myVolume = (4.0 / 3.0) * (Math.PI) * (Math.pow(myRadius, 3));
		
	}
	
	static void getHypotenuse(double a, double b) {
		
		myC = Math.sqrt(Math.pow(myA, 2) + Math.pow(myB, 2));
		
	}
	
}

What I'm confused about is how to test this class. What exactly should I put in the tester class? Did I even do my original class above correctly? Please help! Any feedback is appreciated :D

Recommended Answers

All 9 Replies

Your methods need some work:
You should do the calculations using the values that are passed in as parameters
You should return the answer via a "return" statement.
So - you don't need any of the variables you declared on lines 3-9

When you have fixed these you can write a main method that calls your methods with some test values and prints the returned values.

Thank you JamesCherill! This is what I came up with

class Fun {
	
	public static double fahrenheit(double celsius) {
		
		fahrenheit = ((9.0 / 5.0) * myCelsius) + 32;
		
		return fahrenheit;
		
	}
	
	public static double celsius(double fahrenheit) {
		
		celsius = (5.0 / 9.0) * (myFahrenheit - 32);
		
		return celsius;
		
	}
			
	public static double volume(double radius) {
		
		volume = (4.0 / 3.0) * (Math.PI) * (Math.pow(myRadius, 3));
		
		return volume;
		
	}
	
	public static double hypotenuse(double a, double b) {
		
		hypotenuse = Math.sqrt(Math.pow(myA, 2) + Math.pow(myB, 2));
		
		return hypotenuse;
		
	}
	
	
	
}

I'm still confused on how to test this. Do I need to construct a new object? How do I call the methods?

You still have those myXXX names where you should just have the names of the parameters in the methods, and you have not declared the variables you use for the return values. Compile your code before posting it - the compiler will tell you about errors like those.
Because all your methods are static you don't need to create any objects.
Every Java program needs a "main" method. You must have seen this in your course so far. In your main method you can call your other methods, passing in test data, and print the results they return.

Fixed that!

class Fun {
	
	public static double fahrenheit(double celsius) {
		
		fahrenheit = ((9.0 / 5.0) * celsius) + 32;
		
		return fahrenheit;
		
	}
	
	public static double celsius(double fahrenheit) {
		
		celsius = (5.0 / 9.0) * (fahrenheit - 32);
		
		return celsius;
		
	}
			
	public static double volume(double radius) {
		
		volume = (4.0 / 3.0) * (Math.PI) * (Math.pow(radius, 3));
		
		return volume;
		
	}
	
	public static double hypotenuse(double a, double b) {
		
		hypotenuse = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
		
		return hypotenuse;
		
	}
	
	
	
}

Yep, we've learned about main methods! I'm confused about how to call the methods without creating a new object..?

Here's an example of a relevant JUnit test:

import junit.framework.TestCase;
public class FunTest extends TestCase {
	public void testZeroCelsiusToFahrenheit() {
		double celsius = 0.0;
		assertEquals(32.0, Fun.fahrenheit(celsius));
	}
}

Oh; and this is Java, not FORTRAN: Either add 'double' in front of some of those variables that don't compile, or do 'return <expression>;' instead of 'val = <expression>;' + 'return val;'.

Actually we're not supposed to import anything...

In your main method you can do stuff like
System.out.println(hypotenuse(3.0, 4.0));
if your code is correct it will print the answer 5.

Actually we're not supposed to import anything...

I might do something like...

public static void main(String[] args) {
		assertEquals(32.0, Fun.fahrenheit(0.0));
		System.out.println("*** Green Bar ***  The tests pass!");
	}

	/**
	 * Emulate JUnit functionality which evil instructor will not allow us to use.
	 */
	private static void assertEquals(double expected, double actual) {
		if (actual != expected)
			throw new AssertionError(actual + " != " + expected);
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.