Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-02-2007, 06:07 AM
Member
 
Join Date: Nov 2007
Posts: 1
sc3c is on a distinguished road
hello
hi guys i'm new to java .
can you guys help me with this?
+4'7''
+3'10''
+6'4''
its gone be calculete sum with feet and inches and out come has to be
14 feet9inches
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-02-2007, 11:03 AM
Member
 
Join Date: Jun 2007
Location: Colombo, Sri Lanka
Posts: 31
hiranya is on a distinguished road
This is a simple problem with many possible solutions. I have done it in a very simple way. I first developed the following class.

Code:
public class Length { private int _feet; private int _inches; public Length(int f, int i) { this._feet = f; this._inches = i; } public void showLength() { System.out.println(_feet+"' "+_inches+"''"); } public Length add(Length l) { int inches=l._inches+this._inches; int feet=l._feet+this._feet; if (inches>12) { feet+=inches/12; inches%=12; } return new Length(feet, inches); } }
Then I wrote the following client class to do the addition for me.

Code:
public class LengthAdderClient { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Length l1=new Length(4,7); Length l2=new Length(3,10); Length l3=new Length(6, 4); Length sum=l1.add(l2); sum=sum.add(l3); System.out.print("Length 1: "); l1.showLength(); System.out.print("Length 2: "); l2.showLength(); System.out.print("Length 3: "); l3.showLength(); System.out.print("Sum: "); sum.showLength(); } }
I get the following output on my console.

Code:
Length 1: 4' 7'' Length 2: 3' 10'' Length 3: 6' 4'' Sum: 14' 9''
Hope this helps...

Regards,
Hiranya
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-02-2007, 12:04 PM
Member
 
Join Date: Oct 2007
Posts: 21
unhurt is on a distinguished road
well if u are entering the 4'7" exactly as the input, u can't expect the ' and " to add it up themself so the first thing to do is to take it away and do the math later

u can use str.charat() and follow by an if to check if it is the ' or "
and to ignore that latter when doing addition
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 10:56 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org