Computing.Net > Forums > Programming > Homework assignment

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Homework assignment

Reply to Message Icon

Name: Aimee
Date: November 11, 2005 at 13:54:43 Pacific
OS: Windows XP Home
CPU/Ram: AMD Athlon XP 2500+; 1.5G
Comment:

I am new to JAVA and need some serious help, I need to create a simple program that will give a user the option of converting miles to kilometers or kilometers to miles. I have a lot of starts but I can not seem to get very far. Please if anyone has an example?



Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: November 11, 2005 at 17:39:51 Pacific
Reply:

To convert miles to kilometers multiply by 1.609344. To convert kilometers to miles multiply by 0.6213711.

The rest is easy.

Stuart


0

Response Number 2
Name: Aimee
Date: November 11, 2005 at 17:54:56 Pacific
Reply:

Thank you very much. Unfortunately, I already have that information : ) I am actually stuck on the easy parts. I honestly am confused at the moment as to how to create the program that produces those conversions. Thankyou again. I very much appreciate your quick response!


0

Response Number 3
Name: Mechanix2Go
Date: November 12, 2005 at 01:35:11 Pacific
Reply:

"have a lot of starts but I can not seem to get very far"

What do you have sp far?


If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: Aimee
Date: November 12, 2005 at 07:56:15 Pacific
Reply:

Here is what I have. Please any ideas?

import java.util.*;

public class winchesterProject1
{
public static void main(String[] args)
{
System.out.println("This program will convert miles to kilometers or kilometers to miles.");

int n1;
int enter = 0;
double miles, kilometers;
String answer;
Scanner scannerObject = new Scanner(System.in);

do
{
System.out.println("Enter a number you would like to convert:");
n1 = scannerObject.nextInt();
{
System.out.println("Enter 1 to convert this number to kilometers.");
System.out.println("Enter 2 to convert this number to miles.");
enter = 0;
enter = scannerObject.nextInt();
}
while (n1 > 0)
{
if (enter = 1)
kilometers = n1 * 1.609344;
else if (enter = 2)
miles = n1 * .0621371;
else
System.out.println("Please try again.");
}

System.out.println("Would you like to convert another distance?");
System.out.println("Enter yes or no.");
answer = scannerObject.next();
while (answer.equalsIgnoreCase("yes"));
}
}
}
}
}


0

Response Number 5
Name: Dr. Nick
Date: November 12, 2005 at 23:35:07 Pacific
Reply:

I've noticed several problems:

* You have too many closing braces. The program shouldn't even compile with all those extra braces. Always make sure you have the same number of opening braces as closing braces.

* The WHILE part of the do/while loop should be right after the closing brace for that structure.

* I'm not sure why you have a brace set beginning after the line n1 - scannerObject.nextInt(); and ending before the line while (n1 > 0). Why did you add these in?

* To test for equality you need two = signs (ie: if (enter == 0){})

I've fixed these problems since they are pretty simple and are issues with syntax, not logic. Here's is the code:

import java.util.*;

public class winchesterProject1
{
    public static void main(String[] args)
    { 
        System.out.println("This program will convert miles to kilometers or kilometers to miles."); 

        int n1;
        int enter = 0;
        double miles, kilometers;
        String answer;
        Scanner scannerObject = new Scanner(System.in);

        do
        {
            System.out.println("Enter a number you would like to convert:");
            n1 = scannerObject.nextInt();
            System.out.println("Enter 1 to convert this number to kilometers.");
            System.out.println("Enter 2 to convert this number to miles.");
            enter = 0;
            enter = scannerObject.nextInt();

            while (n1 > 0)
            {
                if (enter == 1)
                    kilometers = n1 * 1.609344;
                else if (enter == 2)
                    miles = n1 * .0621371;
                else
                    System.out.println("Please try again.");
            }

            System.out.println("Would you like to convert another distance?");
            System.out.println("Enter yes or no.");
            answer = scannerObject.next();
        
        } while (answer.equalsIgnoreCase("yes")); 
    }
}

Now for the bad news. You have some logic problems in your code including an infinite loop. I haven't fixed those problems because if you find the mistakes yourself you will be less likely to make them again later on. If after working on it you still have problems post back.


0

Related Posts

See More



Response Number 6
Name: Aimee
Date: November 13, 2005 at 03:05:11 Pacific
Reply:

Thank you very much, I was having a heck of a time placing the while loop. I added a break statement after the final else and it seems to be working fine now Thank you again!!!! : )


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Homework assignment

need help with homework assignment www.computing.net/answers/programming/need-help-with-homework-assignment/15469.html

Homework Crusader? www.computing.net/answers/programming/homework-crusader/8258.html

Have a homework question??? www.computing.net/answers/programming/have-a-homework-question/7407.html