Help! Need some help with a project due in an hour!

Java program in Eclipse

Page 1 of 1

5 Replies - 1336 Views - Last Post: 20 September 2008 - 10:01 PM Rate Topic: -----

#1 absynthe   User is offline

  • DIC Tease
  • member icon

Reputation: 35
  • View blog
  • Posts: 2,877
  • Joined: 20-September 08

Help! Need some help with a project due in an hour!

Post icon  Posted 20 September 2008 - 08:26 PM

I am in a Java class and have an assignment for a conversion program in length. It does not need to be a GUI or applet. The reason for my problem is that I am having a major problems right now on my laptop with Eclipse. I am getting errors in locating the workbench and I tried uninstalling and reinstalling it but NOTHING works. Right now my priority is this code. Its a fairly simple program, I actually did it in C++ last semester. The conversion is nothing. I am having issues with the throwing of exceptions and that is the reason Im not just writing it in Notepad. If anyone can help I would be in your debt! Here is the actual assignment:

Thanks!

Write a program that prompts the user to enter the length in feet and inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers.

When testing, please make sure the program will handle exceptions more than once during any one given run. That is, if I run your program and enter‘d’ for feet and 3 for inches, your program should give me the appropriate exception and when I’m prompted to enter another set of numbers, if I enter a letter again, it should continue to throw the exception until the correct data is entered.

Here is my code from C++. I just need to convert it to Java? I really dont think the exceptions are right either(still some notes from teacher). I had points taken off for that if I remember right. Any help would be a blessing :)

#include <iostream>

using namespace std;

// VARIABLE DECLARATIONS:

double inches; // number of inches
double foot; //number of feet
double centimeters; // number of centimeters
int choice; // users menu choice

int main()
{



//give the user some options and begin actual program

do

{ // while choice is not equal to zero

// present menu of options
cout << endl;
cout << " conversion of inches,feet to cetimeters" << endl;
cout << endl;
cout << "1 - Convert inches and feet to centimeters" << endl;
cout << "0 - Quit" << endl;
cout << endl;
cout << "Enter choice : ";

// get users response
cin >> choice;

if (choice == 1) {

// process inches to centimeters

cout << "Enter length in feet: "<<end1;
cin >> foot;
cout<<"Enter inches as well:"<<end1;
cin>>inches
cout<<end1;

//figure out how many feet and inches is centimeters

//To convert from Inches to Centimeter, simply multiply the Inches by 2.54
//To convert from Feet to Inches, simply multiply the Feet by 12
//To convert from Feet to Centimeters, simply multiply the Feet by 30.48

inches*2.54=inches//total number of centimeters in an inch
foot * 30.48=foot//total number of centimeters in a foot
//add the foot translated to centimeters and the inches translated to centimeters for total foot

foot+inches=centimeters


//output the results

cout << foot<<inches << " foot and inches equals "
<< centimeters << " centimeters." << endl;

//begin throw,try and catch commands--


}
while (choice != 0);



if(inches<=0)
{
throw DivideByZero;
}
return 0;
}
try

//a try should just be the code that could have problems
//below the catch will be the exception created
if inches<=0 then catch
}
catch(float inches)
{
if(inches==DivideByZero;
{
cout<<"Retry entering the numbers";
}
}




return 0; // return to the operating system

} // end of main()


W_W edit: :code:

This post has been edited by William_Wilson: 20 September 2008 - 08:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help! Need some help with a project due in an hour!

#2 absynthe   User is offline

  • DIC Tease
  • member icon

Reputation: 35
  • View blog
  • Posts: 2,877
  • Joined: 20-September 08

Re: Help! Need some help with a project due in an hour!

Posted 20 September 2008 - 09:10 PM

This is the actual finished C+ code I turned in:

#include <iostream> 
: 
: using namespace std; 
: 
: // VARIABLE DECLARATIONS: 
: 
: double inches; // number of inches 
: double foot; //number of feet 
: double centimeters; // number of centimeters 
: int choice; // users menu choice 
: 
: int main() 
: { 
:     //give the user some options and begin actual program 
: 
:     do
:     { // while choice is not equal to zero 
: 
:         // present menu of options 
:         cout << endl; 
:         cout << " conversion of inches,feet to cetimeters" << endl; 
:         cout << endl; 
:         cout << "1 - Convert inches and feet to centimeters" << endl; 
:         cout << "0 - Quit" << endl; 
:         cout << endl; 
:         cout << "Enter choice : "; 
: 
:         // get users response 
:         cin >> choice; 
: 
:         if (choice == 1) 
:         { 
:             // process inches to centimeters 
: 
:             cout << "Enter length in feet: "<<endl; 
:             cin >> foot; 
:             cout<<"Enter inches as well:"<<endl; 
:             cin>>inches;
:             cout<<endl;
: 
: 
:             //figure out how many feet and inches is centimeters 
: 
:             //To convert from Inches to Centimeter, simply multiply 
:             // the Inches by 2.54 
:             //To convert from Feet to Inches, simply multiply the 
:             // Feet by 12 
:             //To convert from Feet to Centimeters, simply multiply 
:             // the Feet by 30.48 
: 
: 
:             //begin throw,try and catch commands-- 
: 
:             try
:             { 
:                 //a try should just be the code that could have problems 
: 
:                 inches*=2.54f;  //total number of centimeters in an inch 
:                 foot *=30.48f; //total number of centimeters in a foot
: 
:                 //add the foot translated to centimeters and the inches 
:                 // translated to centimeters for total foot 
: 
:                 centimeters = foot+inches; 
: 
:                 if(inches<=0)  
:                     throw -1; 
: 
:             //return 0; 
: 
:             }
:             catch (int error) 
:             { 
:                  // handle exception error here 
:             } 
: 
:             //********************************************************************* 
:             //output the results 
: 
:             cout << foot<<inches << " foot and inches equals " 
:             << centimeters << " centimeters." << endl; 
: 
: 
:         } //<- end if 
: 
:     }while (choice !=0);
: 
: 
: // The following code is incorrect.
: /*
: if inches<=0 then catch 
: } 
: catch(float inches) 
: { 
: if(inches==DivideByZero; 
: { 
: cout<<"Retry entering the numbers"; 
: } 
: */
: 
: 
: return 0; // return to the operating system 
: 
: } // end of main() 



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Help! Need some help with a project due in an hour!

Posted 20 September 2008 - 09:12 PM

Project due in an hour and it is now that you wake up ?
12h00 PM North America Eastern Standard time ?
Don't you think you should be a little bit more organized ?

Replace your cout by System.out.println()
Create a Scanner: Scanner in = new Scanner(System.in);
Replace your cin by Double x = in.nextDouble(); and you'll be in business

This post has been edited by pbl: 20 September 2008 - 09:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6839
  • View blog
  • Posts: 31,513
  • Joined: 10-May 07

Re: Help! Need some help with a project due in an hour!

Posted 20 September 2008 - 09:16 PM

View Postabsynthe, on 20 Sep, 2008 - 11:26 PM, said:

I actually did it in C++ last semester

Actually this code isn't yours from last semester.

It can be found on this page & it is from 2005.

Thanks for trying.
Was This Post Helpful? 0
  • +
  • -

#5 absynthe   User is offline

  • DIC Tease
  • member icon

Reputation: 35
  • View blog
  • Posts: 2,877
  • Joined: 20-September 08

Re: Help! Need some help with a project due in an hour!

Posted 20 September 2008 - 09:16 PM

View Postpbl, on 20 Sep, 2008 - 09:12 PM, said:

Project due in an hour and it is now that you wake up ?
12h00 PM North America Eastern Standard time ?
Don't you think you should be a little bit more organized ?



I agree. Totally messed this one up. Been in D.C. all week visiting my little girl and put it off till Sat. Thought I could fix Eclipse :( I screwed up. No ones fault but mine
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5523
  • View blog
  • Posts: 14,523
  • Joined: 18-April 07

Re: Help! Need some help with a project due in an hour!

Posted 20 September 2008 - 10:01 PM

View Postno2pencil, on 20 Sep, 2008 - 09:16 PM, said:

View Postabsynthe, on 20 Sep, 2008 - 11:26 PM, said:

I actually did it in C++ last semester

Actually this code isn't yours from last semester.

It can be found on this page & it is from 2005.

Thanks for trying.


Wicked catch no2...just for that I am definitely not helping. Not that I really would have anyways since I have been burned too many times by these fly by night "I have it due in an hour or two" type of posts and have made it a rule to never help on such notice. But lying about previous code and plagiarizing is just lame.

Shame shame
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1