d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Is There A Simpler Way To Do This?
Add Reply New Topic New Poll
Member
Posts: 2,183
Joined: May 8 2009
Gold: 1.00
Mar 2 2014 03:35pm
I don't want to dive into something I have no idea of doing yet.
But my homework for this question was this, along with the code that I wrote to solve it:

package edu.carrollcc.cis132;

import java.util.Scanner;

/**
* Question 2 [10 Points] Warp fiction.
*
* The year is 2414 and a cannon has been invented that can fire a package into
* space at speeds faster than light. The cannon is also a phase-shifter so the
* package is frictionless and experiences no drag. Only a special landing pad
* can stop the package once it is in flight.
*
* Amazoz plans to use this technology to deliver packages to the Human colonies
* and has promised Same-minute delivery(tm). Amazoz has commissioned you to
* calculate how fast (in light-years per hour) the cannon needs to shoot the
* package to reach the destination in 59 seconds.
*
* The program should input a named destination or a double number indicating a
* light-year distance. The three named destinations include
* "Mars"(0.00000076051 ly), "Tau Ceti e"(11.90 ly), "Gliese 581 d"(20 ly)
*
* The program then outputs the light-years per second (lyph) speed the cannon
* needs to fire. Round the number to 6 decimal places in the output (commas not
* needed).
*
* For example: *User specifies Tau Ceti e Output: 726.101695 lyph
*
* Comments and style are worth up to 2 points.
*
* @author Brad Green
*/
public class Question2 {

public static void main(String[] args) {

final double lightYearPerHour = 61.01694915966;
double distance;
double ly;
String planets;
String planet1 = "Tau Ceti e";
String planet2 = "Mars";
String planet3 = "Gliese";

Scanner keyboard = new Scanner(System.in);

System.out.print("What is the destination or the distance in"
+ " light years: ");

if (keyboard.hasNextDouble()) {
distance = keyboard.nextDouble();
ly = lightYearPerHour * distance;
System.out.print("Calculated speed is: ");
System.out.printf("%.6f", ly);
System.out.print(" lyph. \n");
}
if (keyboard.hasNextLine()) {
planets = keyboard.nextLine();
if (planets.compareToIgnoreCase(planet1) == 0) {
ly = lightYearPerHour * 11.9;
System.out.print("Calculated speed is: ");
System.out.printf("%.6f", ly);
System.out.print(" lyph. \n");
}
if (planets.compareToIgnoreCase(planet2) == 0) {
ly = lightYearPerHour * 0.00000076051;
System.out.print("Calculated speed is: ");
System.out.printf("%.6f", ly);
System.out.print(" lyph. \n");
}
if (planets.compareToIgnoreCase(planet3) == 0) {
ly = lightYearPerHour * 20;
System.out.print("Calculated speed is: ");
System.out.printf("%.6f", ly);
System.out.print(" lyph. \n");

}
else
System.out.println("That is not a valid format.");
}

}
}

Is there a simpler way to do this?
Also, how can I get the input from the JOptionPane method like I did from the scanner class. ex:

input = JOptionPane.showInputDialog("Enter a distance or a planet: ");

So to parse the variable of "input" from a string to an int or double or whatever, how would I do that?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 2 2014 03:54pm
Don't know java, but creating a array of structures would be better. Would just have to loop through the array of structures comparing input string to structure elements to find required distance.

Code
struct planets {
const char *planetName;
const double planetDistance;
}

planets planetList[] = {
{"Mars", 0.00000076051},
{"Tau Ceti e", 11.90},
{"Gliese 581 d", 20}
};

//get input from stdin

for(int i = 0; i < sizeof(planetList) / sizeof(struct planets); ++i) {
if(strcmp(input, planetList[i].planetName) == 0) {
distance = planetList[i].planetDistance;
break;
}
}

//Calculate for distance.


Alternatively I would image java would have implemented tables/lists/hashes where you can create a list of string indexes with string/numeric values.

Code
planetList = {"Mars" => 0.00000076051, "Tau Ceti e" =>11.90, "Gliese 581 d" =>20}

if planetList.include? userInput
distance = planetList[input]
end

#do calculations


Just some examples in other languages that should be fairly straight forward which will allow for easier maintenance/upkeep for larger lists if java has suitable equivalents.

This post was edited by AbDuCt on Mar 2 2014 03:56pm
Member
Posts: 2,183
Joined: May 8 2009
Gold: 1.00
Mar 2 2014 03:58pm
Thanks, I semi understand. I'm just starting on increments this week. I havn't touched arrays at all yet, but I have briefly seen them in the book.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 2 2014 04:01pm
Quote (Kellye @ Mar 2 2014 05:58pm)
Thanks, I semi understand. I'm just starting on increments this week. I havn't touched arrays at all yet, but I have briefly seen them in the book.


Essentially you are just creating a lookup table where you can search for the name of the planet a user inputted to grab its value from the table. Unfortunately I don't know the name of these for java. After a quick google search looks like HashMap() is the function you are looking for if you want to implement something like this:

Code
private static final Hashtable<String,Integer> MYHASH = new Hashtable<String,Integer>() {{
put("foo", 1);
put("bar", 256);
put("data", 3);
put("moredata", 27);
put("hello", 32);
put("world", 65536);
}};


I'm sure someone else will come along and add more needed details or a better method for storing string, double indexes.

Using this method will eliminate allot of your if/elseif parsing you are doing.

More information about hashmaps can be found here:

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

This post was edited by AbDuCt on Mar 2 2014 04:03pm
Member
Posts: 2,183
Joined: May 8 2009
Gold: 1.00
Mar 2 2014 04:05pm
Yea, I'm pretty sure the prof. wanted us to parse all the if/else if statements. I was just mainly curious because I enjoy programming.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 3 2014 08:21am
Code
public class Question2 {

final static double LIGHT_YEAR_PER_HOUR = 61.01694915966;

public static void main( String[] args ) {

Map<String, Double> planets = new TreeMap<String, Double>(
String.CASE_INSENSITIVE_ORDER );
planets.put( "Tau Ceti e", 11.9 );
planets.put( "Mars", 0.00000076051 );
planets.put( "Gliese", 20.0 );

Scanner keyboard = new Scanner( System.in );

System.out.print( "What is the destination or the distance in"
+ " light years: " );

if ( keyboard.hasNextDouble( ) ) {
System.out.print( "Calculated speed is: " );
System.out.printf( "%.6f", LIGHT_YEAR_PER_HOUR * keyboard.nextDouble( ) );
System.out.print( " lyph. \n" );
} else {
Double distance = planets.get( keyboard.nextLine( ) );
if ( distance == null ) {
System.out.println( "Planet not found" );
return;
}
System.out.print( "Calculated speed is: " );
System.out.printf( "%.6f", LIGHT_YEAR_PER_HOUR * distance );
System.out.print( " lyph. \n" );
}
}
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll