constructors

if where i declared my constructor is write how come it isn't work

Page 1 of 1

1 Replies - 1070 Views - Last Post: 04 September 2007 - 04:22 AM Rate Topic: -----

#1 e_okandeji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-June 07

constructors

Post icon  Posted 04 September 2007 - 04:00 AM

Hi all i am emmanuel,
pls am new to java and i have a problem with this particular question....

Write a program with a Rational class. The purpose of the Rational class is to manipulate rational number operations. A rational number is a number that can be expressed in the form A / B where A and B are both whole numbers (no fractions or decimals) and B must not be equal to 0.

Your Rational class needs to declare two data attributes: num for numerator and den for denominator. Only one constructor is required, which uses two parameters entered at the keyboard. The first parameter is the numerator and the second parameter is the denominator. The Rational class requires three additional methods, which are getDecimal, getNum and getDen. The getDecimal method returns the decimal value of the current rational number that is stored. If the number 3 / 4 is stored, then getDecimal will return 0.75. Method getNum returns the numerator and method getDen returns the denominator.

well here is how far i have gone with it. the coments here are questions of where i do not understand

import java.text.DecimalFormat; 
public class Assignment08 
{
	public static void main(String args[]) 
	{
		System.out.println("LAB08A 70 POINT VERSION");
		System.out.println();
		Rational r = new Rational( Integer.parseInt(args[0]),Integer.parseInt(args[1]) );
		
		System.out.println("Two parameter constructor");
		System.out.println(r.getNum() + "/" + r.getDen());
		System.out.println(r.getDecimal());
		System.out.println();
		
	}

}
class Rational
{
		
		private int Numerator;
		private int Denorminator;
		private double d; 
		
			
	public Rational(int a, int b)
	{
		Numerator = a;
		Denorminator = b;
		//DecimalFormat d = new DecimalFormat(0.00);	 // where do i paste my constructor.
		d = a / b;
		   	
	}
	
	public int getNum()		 {return Numerator;}
	public int getDen()		 {return Denorminator;}
	
	public double getDecimal()  
	{
		DecimalFormat e = new DecimalFormat(0.00);	   // if i put it here it still 
		return e.format(d);				 // wouldn't work.
	}
	  
}


if i run the program i get a compile error, -- cannot find symbol constructor DecimalFormat(double) in line 48 and also incompatable types in line 49.
pls i really dont just need the correct or right codes pls i do also need some brief explanation on where am wrong.
thanks ....

*admin edit: code tags

This post has been edited by e_okandeji: 04 September 2007 - 04:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: constructors

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: constructors

Posted 04 September 2007 - 04:22 AM

Please describe your problem a bit more accurately! Do you receive a compiler error(please copy it here)? Do you have some unexpected output (if yes, please specify what do you enter what do you get and what would you expect)?
The constructor for the Rational class seems to be at the right place, however I've noticed that you have some problems in it:
public Rational(int a, int B){
Numerator = a;
Denorminator = b;


Java is case sensitive, so if oyu declare 'B' it won't recognize 'b'.

And please next time use code tags to enter your code!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1