Šta je novo?

Java program (potrebna pomoc)

Northwood

Slavan
Učlanjen(a)
09.10.2005
Poruke
1,822
Poena
349
OK, potrebna mi je pomoc oko programiranja jednog programcica 😉
Dobio sam zadatak da odradim program koji ce da preracuna broj milja u svetlosne godine i AU (Astronomical Unit) i da to prikaze na odredjen nacin...

Ukoliko nekoga ne mrzi da procita ovo i da mi pomogne

Purpose: A light year is the distance in which light can travel in a vacuum in the course of one year. It is a unit of measure that helps scientists to measure distances in the universe where distances are very large. One light year is 5,850,000,000,000 miles (or 5.85* 1012). (from HowStuffWorks.com)

Astronomical units (AU) are used to measure distances within our solar system and refer to the average distance between earth and sun. One light year = 63,239.7 AU.

Your task is to develop an application that will calculate the number of astronomical units and light years from earth to various bodies in the universe given the number of miles to that body.


Program Behavior

Your application must do these things:

1. Your program will prompt for and read in two input value; the name of the celestial body we are working with and the distance in miles from earth to the given body.
2. Your program will calculate the appropriate light years and AUs to that body.
3. Your program must display these results.

Output

1. Output the heading, “Welcome to the CS139 Universe Distance Calculator” followed by the two newline characters.
2. Your first input prompt must be the string, “Type the name of the astronomical body: “
3. You must output a new line character after the user has typed in the name.
4. Your second input prompt must be the String, name " is how many miles away? ", where name is the response to the first prompt.
5. You must output a new line character after the user has typed in a name.
6. Your final output line must be preceded by one line of blank space and then must be of the form:

"%s is %.2f light years and %.2f astronomical units from Earth\n",

where the first substitution is the name of the body, the second is the number of light years and the final substitution is the number of AUs.


Example Output
Kod:
Welcome to the CS139 Universe Distance Calculator

Type the name of the astronomical body: Alpha Centauri

Alpha Centauri is how many miles away? 25155000000000


Alpha Centauri is 4.30 light years and 271930.71 astronomical units from Earth.

Additional Program Requirements

1. You must use variables for the values of the input parameters.
2. You must use constants for the two conversion factors.
3. Numbers displayed as output should be output with 2 decimal points and no commas.
4. All divisions carried out should be floating point division regardless of the data type of the operands.
5. Your program must include an acknowledgement section acknowledging help received from TAs or reference sources. You do not need to reference receiving help from the instructor. See this course's Style Guide for other style criteria.
6. If you received no help your acknowledgement section should have a statement to that effect.
7. Your program must conform to standard Java programming standards and the additional standards for this class. See the Style Guide for this class.

imam problem sa konverzacijom (sta da postavim tacno kao operand-e i output) 😀

za sada kod izgleda ovako
Kod:
import java.util.Scanner;

public class HowFarIsIt
{
	/********************************************
	 * main method documentation goes here.  What does
	 * this method do 
	 *
	 * @param args Unused in this application
	 ********************************************/
	public static void main (String args [])
	{
		String aBody; // User input
		String miles; // Number of miles as a literal
		float numOfMil; // Number of miles as a number
		float lY; // Light Years
		float aU; // Astronomical Units
	
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println("Welcome to the CS139 Universe Distance Calculator");
		
		System.out.println("\n");
		System.out.println("\n");
		
		System.out.print("Type the name of the astronomical body: ");
		aBody = keyboard.nextLine();
		
		System.out.println("\n");
		
		System.out.print(aBody + " is how many miles away? ");
		miles = keyboard.nextLine();
		numOfMil = Float.parseFloat(miles);
		
		System.out.println("\n");
		System.out.println("\n");
		
		// ACTION CODE:
		
	}
}

ako sam izostavio nesto samo recite...

Hvala
 
Ne vidim u čemu je problem. Učitao si vrednost koju je uneo korisnik, parsirao si je u float vrednost, još treba da prevedeš tu vrednost u svetlosne godine i astronomske jedinice i to je to, ukoliko sam dobro razumeo problem.
 
Ne vidim u čemu je problem. Učitao si vrednost koju je uneo korisnik, parsirao si je u float vrednost, još treba da prevedeš tu vrednost u svetlosne godine i astronomske jedinice i to je to, ukoliko sam dobro razumeo problem.

ma sve je bilo ok, nego sam ja lose napisao action code (tj. konverzacije izmedju milja, godina i AU) pa linux server i submit komanda nije htela da prepozna program kao validan...😀 ali, prepravio sam to i sada je sve ok 😉

evo ako nekog zanima kako izgleda finalna verzija:

Kod:
import java.util.Scanner;

/**************************************
 * This application will calculate the number of
 * astronomical units (AU) and light years from Earth 
 * to the other planets/stars in the Universe.
 *************************************/

public class HowFarIsIt
{
	/********************************************
	 * calculate the number of
	 * astronomical units (AU) and light years from Earth 
	 * to the other planets/stars in the Universe 
	 * (in miles).
 	 *
	 * @param args Unused in this application
	 ********************************************/
	public static void main (String args [])
	{
		String aBody; // User input
		String miles; // Number of miles as a literal
		double numOfMil; // Number of miles as a number
		final double LIGHT_YEARS; // Light Year Constant
		final double A_U; // Astronomical Unit constant
		double lY; // Light Years calculated
		double aU; // AU calculated
		Scanner keyboard;
	
		// Creating constants
			LIGHT_YEARS = 5.85 * Math.pow(10,12);
			A_U = LIGHT_YEARS / 63239.7;
	
		keyboard = new Scanner(System.in); // New Scanner Object
		
		System.out.println("Welcome to the CS139 Universe Distance Calculator");
		
		System.out.print("\n");
		
		System.out.print("Type the name of the astronomical body: ");
		aBody = keyboard.nextLine();
		
		System.out.print("\n");
		
		System.out.print(aBody + " is how many miles away? ");
		miles = keyboard.nextLine();
		numOfMil = Double.parseDouble(miles);
		
		System.out.print("\n");
		System.out.print("\n");
		
		// Action code
			lY = numOfMil / LIGHT_YEARS;
			aU = numOfMil / A_U;
		
		System.out.printf("%s is %.2f light years and %.2f astronomical units from Earth.\n"
			, aBody, lY, aU);
		
	}
}

:wave:
 
opet ja 😀
treba mi mala pomoc oko jednog vrlo kratkog programcica...
naime, program treba da bude niz od 3 elementa, a da svaki element ima kvadrat svoje vrednosti na kraju
uputstva su:
- napravim niz (array) od tri elementa ([0], [1], [2]) koji ce da sadrze vrednosti 0, 1, 4...
- deklarisem niz kao double squares...
- napravim objekat u memoriji uz pomoc "new" keyword-a...
- napravim for petlju koja ce se izvrsiti 3 puta i u cijem ce delu da bude jedna linija koda koja izgleda ovako: square[ii] = ii * ii
- i na kraju jos jedna for petlja koja ce se izvrsiti 3 puta i koja ce da prikaze rezultat uz pomoc jedne System.out.println naredbe

Kod:
public class HW061108
{
   public static void main(String[] args)
   {		
		double[] squares;
		final int LENGTH = 3;
		
		squares = new double[LENGTH];
		
		squares[0] = 0;
		squares[1] = 1;
		squares[2] = 4;

		System.out.println("Feeding numbers!");
		
		for (int ii = 0; ii < LENGTH; ii++)
		{
		squares[ii] = ii * ii;
		}

                System.out.println("Printing results");
			
		for (int ii = 0; ii < LENGTH; ii++)
		{
			System.out.println(square[ii]);
		}
   }
}
ovo gore je dobro sve dok ne treba da napisem prvu petlju, jednostavno nece da napravi kvadrat broja uz pomoc square[ii] = ii * ii, a ne znam sta da popravim kako bih dobio zeljene rezultate...
treba mi pomoc oko pravljenja navedenih for petlji, pa ako je neko raspolozen da mi pomogne...

:type::wave:
 
Poslednja izmena:
Od niza brojeva 0, 1, 4 treba da generises niz sa njihovim kvadratima 0, 1, 16, ako sam dobro shvatio ?

Kod:
for (int ii = 0; ii < LENGTH; ii++)
		{
		[U][B]squares[ii] = ii * ii[/B][/U]; }

Ova linija ne radi kvadriranje, vec dobijas isti rezultat kao ono pojedinacno deklarisanje clanova niza gore, tj. samo si generisao niz.

Mislim da ovako treba da izgleda:

Kod:
for (int ii = 0; ii < LENGTH; ii++)
		{ squares[ii] = squares[ii] * squares[ii]; }
 
Poslednja izmena:
Od niza brojeva 0, 1, 4 treba da generises niz sa njihovim kvadratima 0, 1, 16, ako sam dobro shvatio ?

Kod:
for (int ii = 0; ii < LENGTH; ii++)
		{
		[U][B]squares[ii] = ii * ii[/B][/U]; }

Ova linija ne radi kvadriranje, vec dobijas isti rezultat kao ono pojedinacno deklarisanje clanova niza gore, tj. samo si generisao niz.

Mislim da ovako treba da izgleda:

Kod:
for (int ii = 0; ii < LENGTH; ii++)
		{ squares[ii] = squares[ii] * squares[ii]; }

thx colt, to mi je promaklo 😉
 
Nazad
Vrh Dno