Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Write a currency converter class program

  1. #1
    Junior Member
    Join Date
    Aug 2022
    Location
    Malaysia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a currency converter class program

    Hello All,

    I am Java course student and having a hard time writing a code for the following question. I need a piece of advice on how to write this as per requirement. Thank you

    Write a currency converter class program based on the information given below. Add TWO more suitable and unique member methods for the above class. Ensure these new methods do not functionally overlap with the existing methods. You are not allowed to add additional attributes. Provide reasons in the form of “program comments” (80-150 words) on why you choose these methods.

    Class Name: CurrencyConverter
    Attribute : exchangeRate -Exchange rate to convert a US Dollar to ringgit
    Constructor: to set the initial value for the attribute
    Member methods:
    • double fromRinggit (…)
    -Converts a given amount in ringgit into an equivalent amount in US Dollar

    • double toRinggit(..)
    -Converts a given amount in the US Dollar into an equivalent ringgit amount

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Write a currency converter class program

    What have you tried so far?
    Do you have any specific questions about the program?

    Please be sure to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2022
    Location
    Malaysia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a currency converter class program

    Hello Norm,

    Sorry i didn't mean to but I'm pretty much stuck with this one for a few days. I have completed the other 5 questions but don't even know where or how to start this one. I just have a sample code which I don't know how to modify.

    --- Update ---

    The one code that is working is the one below

    import java.util.*;
    import java.text.DecimalFormat;
     
    public class CurrencyConverter {
     
    	public static void main(String[] args) {
     
    		double amount, dollar, code, ringgit ;
     
    		DecimalFormat f = new DecimalFormat("##.##");
     
    		@SuppressWarnings("resource")
    		Scanner sc = new Scanner(System.in);
     
    		System.out.println("USD & MYR Currency Conventer");
     
    		System.out.println("Which currency You want to Convert ? ");
     
    		System.out.println("1:Dollar \t2:Ringgit ");
    		code = sc.nextInt();
     
    		System.out.println("How much Money you want to convert ?");
    		amount = sc.nextFloat();
     
    		// For amounts Conversion
    		if (code == 1) {
     
    			// For Dollar Conversion
     
    			ringgit = amount * 4.17;
    			System.out.println("Your " + amount + " Dollar is : " + f.format(ringgit) + " ringgit");
     
    		} else if (code == 2) {
    			// For Dollar Conversion
     
    			ringgit = amount * 4.17;
    			System.out.println("Your " + amount + " Dollar is : " + f.format(ringgit) + " ringgit");
     
     
    		} else {
    			System.out.println("Invalid input");
    		}
     
    		System.out.println("Thank you");
    	}
     
    }
    Last edited by pubalan; August 5th, 2022 at 11:24 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Write a currency converter class program

    The one code that is working is the one below
    Sorry, I don't understand what your questions are about that code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2022
    Location
    Malaysia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a currency converter class program

    How can integrate the following requirements from the question into the code? I don't know how to come up with a new code for this question sadly.


    Attribute : exchangeRate -Exchange rate to convert a US Dollar to ringgit
    Constructor: to set the initial value for the attribute
    Member methods:
    • double fromRinggit (…)
    -Converts a given amount in ringgit into an equivalent amount in US Dollar

    • double toRinggit(..)
    -Converts a given amount in the US Dollar into an equivalent ringgit amount

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Write a currency converter class program

    Let's do it a few items at a time. Make the changes, compile and execute for test.
    When that code works, move to the next items.
    Attribute : exchangeRate -Exchange rate to convert a US Dollar to ringgit
    I assume that refers to the 4.17 number that is hard coded in the program.
    Add a variable to the class that holds the rate and use that variable in the computations instead of the hard coded 4.17
    Constructor: to set the initial value for the attribute
    Add a constructor to the class that has the exchange rate as its argument. Have the constructor save that value in the variable mentioned above.

    Adding these 3 lines immediately after the "public static void main..." statement will give the class a constructor that can be modified as needed:
          new CurrencyConverter();                     //  ADDED these 3 lines
       }  // end main()
       public CurrencyConverter() {

    Is this the same problem: https://coderanch.com/t/753313/java/...r-Java-program
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: December 1st, 2018, 03:39 PM
  2. Replies: 15
    Last Post: May 2nd, 2013, 05:29 AM
  3. Replies: 0
    Last Post: February 20th, 2012, 02:04 PM
  4. GUI temp converter program troubles
    By copelandtml in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 14th, 2011, 10:28 AM
  5. J2ME converter program
    By sackling in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 6th, 2010, 09:13 AM