View Full Version : help in java coding
aal300m
05-29-2006, 04:05 PM
I am trying to define a java class named NumberCruncher that has a single int variable as its only instance variable. Then I have to define methods that perform the following operations on its number: get, double, triple, square, and cube. Sent the intial value of the number with a constructer.
Can anyone help me with this. It will be deeply appreciate it.
I'm assuming this is homework for a beginner course in java. Since this task is rather simple I'll also assume that the problem lies in the fact that you don't know how to construct a class. So I'll give you a skeleton for this problem and allow you to fill in the missing code.
NumberCruncher.java
public class NumberCruncher{
private int number;
public NumberCruncher(int num)
{
//set number equal to num
}
public int getNumber()
{
//return the original number
}
public int doubleNumber()
{
//calculate and return double the original number
}
public int tripleNumber()
{
//calculate and return triple the original number
}
public int squareNumber()
{
//calculate and return the square of the number
}
public int cubeNumber()
{
//calculate and return the cuber of the number
}
}
testNumberCruncher.java
public class testNumberCruncher{
public static void main (String[] args){
NumberCruncher test = new NumberCruncher(2);
System.out.println("The number in the cruncher is " + test.getNumber());
System.out.println("Double the number is " + test.doubleNumber());
System.out.println("Triple the number is " + test.tripleNumber());
System.out.println("Square of the number is " + test.squareNumber());
System.out.println("Cube of the number is " + test.cubeNumber());
}
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.