Wrox Programmer Forums
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 5th, 2007, 10:21 AM
Authorized User
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chris1012
Default java program help

hey guys i was wondering if you could help me basically at the minute am working on an excercise with java programming for my Foundation Degree Course in computing. i've been working with the basics of java ie defining variables and setting up the jdk kit i've got a list of excercises which i have been asked to complete. the excercises are based on converting values ie miles yards and so on (college computers suck the jdk kit isn't even installed propperly on them) but what i've created so far is the Main.java file and the actual file itself which is named variable i would just like a bit of info on how to define this in a java based program note i am using edit plus when created java applications because at the minute i am only working with the command prompt at this stage the code i have got is as follows

class Main
{
    public static void Main(String[] args)
    {
    Scale scale =new Scale();
}
}

class Scale
{
    public Scale();
    {
        double mile=3.5
        int yards=6160
        int feet=18480
        int inches 221760
        System.out.println(miles * yards * feet * inches);

    }
}
the second section of code indicates the variables which i have setup what i have been asked is to convert from inches to feet and yards and miles. am not expecting anyone to do my work for me i just feel i need to grasp a better understanding of how the code works and what sort of code is used to carry out the calculation :( please help anyone i beg of you am dieing here:(

by the way sorry about the bad spelling afterall i am student lol

in opposite world i love programming
__________________
in opposite world i love programming
 
Old November 5th, 2007, 04:00 PM
Registered User
 
Join Date: Sep 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First, you need to make sure you use the right syntax. The compiler won't understand it otherwise. In Java that means never leaving out an equals sign and putting a semicolon after every statement. This will get your code to compile.

Next, be sure to pick good names for things. Instead of "yards", if you have a scaling factor to convert from a mile to yards, call it something like mileInYards.

Finally, make sure you understand what has to do with programming and what has to do with your domain (in this case, math). You may find it easier just to write the basic arithmetic and then translate it to Java.

Code that doesn't compile but illustrates what you're trying to do is called "psuedocode".

Finally, if you have a bad attitude about learning, you will have a harder time making the grade. Try to enjoy it.

 
Old November 5th, 2007, 04:37 PM
Authorized User
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chris1012
Default

thanks m8 am just finding it difficult what code to use ie how to configure the layout and multiply what i need to sort of thing this java melarky tends to eb confusing lol but i will percivier thanks for your help

in opposite world i love programming
 
Old November 8th, 2007, 09:35 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

It can help to think of objects or classes as nouns/things and methods as verbs/actions.

If the question is "create a class that can convert a distance in miles to a distance in yards", perhaps what you're really after is a class called distanceInMiles (which is the thing in this sentence). You want that distance in miles to be able to tell you how far it is in yards - that's an action. So, you can make a method called toYards() which spits out the distance in yards. You can later add methods to spit out the distance in feet, inches, centimeters or whatever whenever you need to.

So as a rough idea:

Code:
public class distanceInMiles {
  float distanceInMiles;

  public distanceInMiles(float dist) {
    distanceInMiles = dist;  // Now we know how far we are in miles
  }

  public float toMiles() {
    return distanceInMiles;
  }

  public float toYards() {
    return distanceInMiles * 1760f;
  }

  public static void main (String argv[]) {
    distanceInMiles m = new distanceInMiles(1);
    System.out.println(m.toYards() + " yards");
  }
}
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk





Similar Threads
Thread Thread Starter Forum Replies Last Post
java program with package sushant2002 Java Basics 5 September 19th, 2008 09:15 PM
java program todeepak_g JSP Basics 0 January 25th, 2006 06:04 AM
java program todeepak_g JSP Basics 0 January 25th, 2006 06:01 AM
How use two different database in one java program [email protected] Java Databases 2 October 3rd, 2005 01:07 PM
My Java program won't compile emilaghayev JSP Basics 0 October 21st, 2003 06:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.