Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,307 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 793 people online right now. Registration is fast and FREE... Join Now!




Metric converter

 
Reply to this topicStart new topic

Metric converter

bsq935
6 Oct, 2008 - 06:43 PM
Post #1

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 33


My Contributions
Ehhhh sorry guys x.x I totally though I could do this one without help.

Anyway. This class converts metric units to SI I think it's called? And it doesn't convert for some reason sleep.gif

Here's what I have.

CODE
public class Length{
  
    //PRIVATE INSTANCE VARIABLES
    private double myNumber;
    private double myUnit;
    
    private double inches;
    private double feet;
    private double yards;
    private double miles;
    private double meters;
    
    public int inch1;
  
  //CONSTRUCTOR
  
  /** This constructs a length with a number and a unit assigned to it
      @param number The number of the length
      @param unit The unit of the length
   */
  public Length(double number, String unit){
    //Constructs a length
    //Defines inches, feet, yards, and miles
      
    inches = meters * 39.37;
    feet = meters * 39.37 / 12;
    yards = meters * 39.37 / 12 / 3;
    miles = meters * 39.37 / 12 / 3 * 1760;
    
   inches = getInches();
   feet = getFeet();
   yards = getYards();
   miles = getMiles();
    
  }
  
  //METHODS
  
  /** Calculates the number of inches
      @return inches The number of inches
   */
  public double getInches(){
    //Indicates the number of inches
    
    return inches;
  }
  /** Calculates the number of feet
      @return The number of feet
   */
  public double getFeet(){
    //Indicates the number of feet
    return feet;
  }
  
  /** Calculates the number of yards
      @return The number of yards
   */
  public double getYards(){
    //Indicates the number of yards
    return yards;
  }
  
  /** Calculates the number of miles
      @return The number of miles
   */
  public double getMiles(){
    //Indicates the number of miles
    return miles;
  }
  /** Sets the number for the length
      @param number The length's number
    */
  public void setNumber(double number){
    //Assigns a number to the length
    
  }
  /** Sets the unit for the length's number
      @param unit The unit of the length's number
    */
  public void setUnit(String unit){
    //Assigns a unit to the length's number
    
  }
  /** Constructs a string to represent the numbers
      @param s The String
   */
  public String toString(){
    //Makes a string that looks like:
    //There are "..." meters in a yard
    String s = meters + "---";
    s += inches;
    s += feet;
    s += yards;
    s += miles;
    s += meters;
    return s;
  }
}


I couldn't figure out the method bodies for setNumber and setUnit, that may be my only problem...

User is offlineProfile CardPM
+Quote Post


stauffski
RE: Metric Converter
6 Oct, 2008 - 07:12 PM
Post #2

D.I.C Head
**

Joined: 3 Nov, 2007
Posts: 161



Thanked: 20 times
My Contributions
So, I was looking at your code, and it is a little choppy. I believe you think it is doing something it isn't. Maybe a late night, lol. Here is what I think you might want to change.
java

//in your constructor
public Length(double number, String unit){
//why do you send double number and String unit if you don't use them?
//meters isn't defined yet, why are you using it?
inches = meters * 39.37;
feet = meters * 39.37 / 12;
yards = meters * 39.37 / 12 / 3;
miles = meters * 39.37 / 12 / 3 * 1760;

//the following does nothing, take a closer look
inches = getInches();
feet = getFeet();
yards = getYards();
miles = getMiles();
}
//consider instead making a methods like the following

public double toSIInches(double meters){
}
public double toSIFeet(double meters){
}
public double toSIYards(double meters){
}
public double toSIMiles(double meters){
}


Start with that.

Good Luck!
User is offlineProfile CardPM
+Quote Post

bsq935
RE: Metric Converter
6 Oct, 2008 - 08:34 PM
Post #3

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 33


My Contributions
Oh I guess I was just using double number and String unit in the LengthTest... Would I use them in the same way in the Length interface?

Like... would meters = number; make any sense at all? I guess I'm trying to connect that back to the constructor parameters. Like...
CODE
public length(double number, String unit){
            meters = number;


Eep thirty minutes left, I may have to admit defeat in this one. Thanks again, though!

I know, I have some studying to do x.x
User is offlineProfile CardPM
+Quote Post

stauffski
RE: Metric Converter
6 Oct, 2008 - 08:57 PM
Post #4

D.I.C Head
**

Joined: 3 Nov, 2007
Posts: 161



Thanked: 20 times
My Contributions
It makes sense if your intentions are to send the number of meters in the constructor. However, number is uninformative to the user. Try using different variable names to be clearer.

java

public length(double meters, String unit){
recievedMeters = meters;
}

Also, it is not clear what you want with unit. I would recommend keeping the constructor using only meters and leaving the metric conversion up to the method that uses the Length class.

Sorry if this does not answer you, it is late in my timezone, lol tongue.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:47PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month