Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Need Help With Java Programming

Options
  • 28-01-2009 9:05pm
    #1
    Registered Users Posts: 79 ✭✭


    Hi Everyone,
    This harry potter stuff has to go into Java Creator and then put into java coding and displayed on a black screen after its compiled and Executed to the black screen. Anyway i'm only starting this and i'm abit lost and i got stuck and thought i would ask if anyone knew what kind of way do i have to put this into the java code coz i'm confused and lost.Heres more info to go with it. Its the harry potter bit at the end i'm lost with and what way to put it in the java coding.
    the harry potter has to go in the same way but leave out the - bit.the rest the same way.Its just the coding of it i need help with and i know some people mite say it said it, well i don't understand it so sorry.:confused:


    Program Analysis:

    • Line 8 initialises 2 String variables to the values indicated. Note that String is spelt with an uppercase ‘S’. This is because String is actually a class in Java, whereas int ,float and double are called primitive data types.

    Note that String variables are assigned pieces of text - String constants – which are always enclosed in double-quotes, just like the text messages in the println(). In fact the text messages in a println() are also String constants.


    Exercise 1

    Write a program called Exercise1.java that creates appropriate variables for storing the following information for a book:

    Title – Harry Potter and the Prisoner of Azkaban
    Author – J.K. Rowling
    Pages – 317
    Year of Publication – 1999
    Price – 35.50


    Program Analysis:

    • The program begins with 2 comments as normal

    • A class definition follows and then the main() method definition as always.

    • Line 8 initialises two floating-point variables called stockIndexISEQ and stockIndexFTSE with values of 4325.56f and 5423.34f respectively.

    Initialisation of a variable means declaring it and giving it an initial value in the same statement. Recall in the IntegerVariables.java how we separated these steps.

    Naturally it is more efficient to initialise variables wherever possible.

    Note also in line 8 that the 2 variables have been initialised together in the same statement, separated with a comma.

    One thing that looks quite odd here is that the numbers are 4325.56f and 5423.34f. What is the ‘f’ for here? In Java, this ‘f’ is essential in order to avoid a certain kind of syntax error. This comes about because, in Java, there are 2 floating-point types – float, which we have just used here, and double, which means “double-precision floating-point”.

    double is capable of storing values to a much greater degree of accuracy than float can and can also store a much wider range of values than float. We will use double at certain points in this module also but, in many cases, a float variable suffices for us because the number we are dealing with is relatively small and does not require huge accuracy. It is more efficient to use float rather than double wherever possible. The way the Java compiler distinguishes between float constants and double constants is by having the ‘f’ tagged onto the end of a float constant, as we have done here.

    • Finally, the println() method displays the stock index values to the screen and the format here is exactly the same as in the integer variable case, with a \n used to put the values on separate lines on the output window.


    Naming Identifiers in Java

    Collectively, variable names, class names and method names are referred to as identifiers (since they allow us to identify certain things within a program). There are a number of general rules which must be followed if you are naming identifiers in Java as follows:

    (a) Identifiers must begin with a letter or underscore
    (b) Identifiers can only contain letters, digits, underscore or currency symbols
    (c) Identifiers cannot contain a blank
    (d) Identifiers cannot be reserved words (keywords)

    What was said about making identifiers meaningful is just as important in Java as it is in Just BASIC.


    Using String Variables in Java Programs

    Recall what a string is and what a string variable can hold.



    Also heres what i figured out so far but i know its wrong but i'm just lost anyway and java programming hard anyway.

    //Excercise1.java
    /*This program declares 2 integer variables, gives them value and the displays
    *the variables*/

    public class Exercise1 {
    public static void main(String args[])
    {

    String groups ("Harry Potter and the prisoner of Azkaban");


    System.out.println("Title:");
    }
    }


Comments

  • Registered Users Posts: 4,188 ✭✭✭pH


    the harry potter has to go in the same way but leave out the - bit.the rest the same way.Its just the coding of it i need help with and i know some people mite say it said it, well i don't understand it so sorry.:confused:

    I don't understand it either, hopefully others will come along and we'll all not understand it together.


  • Registered Users Posts: 5,112 ✭✭✭Blowfish


    The descriptions are confusing so I'll make several assumptions, namely that what you are attempting to achieve is this and everything else can be ignored for now:
    Write a program called Exercise1.java that creates appropriate variables for storing the following information for a book:

    Title – Harry Potter and the Prisoner of Azkaban
    Author – J.K. Rowling
    Pages – 317
    Year of Publication – 1999
    Price – 35.50

    The program will then print these variables to the command line.
    Would that be correct?


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    yeah, what the hell is going on here...harry potter and stock indexes??

    not to mention, answers to class assignments aren't generally provided here...

    you need to initialise the variables, assign the values to the variables, and then print the variables e.g.
    public class Exercise1
    {
       public static void main(String[] args)
       {
          int Title, Author, Pages, etc.;
    
          Title = Harry Potter;
          etc.
    
          System.out.println(Title);
       }
    } 
    


  • Registered Users Posts: 527 ✭✭✭Sean^DCT4


    Hi Everyone,
    This harry potter stuff has to go into Java Creator and then put into java coding and displayed on a black screen after its compiled and Executed to the black screen.

    AKA DOS-Prompt / Command-Prompt?


    A few suggestions:
    If you aren't interested in the course, or find you are spending a lot of time working on exercises such as this one compared to your peers then maybe you should consider changing course/module.


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    I agree with the above^^^
    Mirror wrote: »
    int Title, Author, Pages, etc.;

    Title = Harry Potter;
    etc.

    System.out.println(Title);

    Tell me you were drunk :p


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    grasshopa wrote: »
    I agree with the above^^^



    Tell me you were drunk :p
    lol, i have never ever done java, and also wasn't handing her the answer so i was secretly hoping she would copy pasta :P


  • Closed Accounts Posts: 1,015 ✭✭✭Epic Tissue


    WTF

    I assume you just have to make a few variables of correct type.. so title/author would be String, year/pages would be int and price would be double or float


  • Registered Users Posts: 163 ✭✭stephenlane80


    Can you post the exact question if its a homework assignment, it might be easier to decipher !!

    I didnt know harry potter was a stock trader !!


Advertisement