Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Conversion between primitive data types: mixed mathematical expressions

  1. #1
    Junior Member codingninja's Avatar
    Join Date
    Aug 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Conversion between primitive data types: mixed mathematical expressions

    Hi everyone.

    This is my first post. I read over the rules for posting and this section seemed like the best place to ask my question, however if it is not please direct me to the correct place!

    A bit of back story so you understand why I'm asking such an odd question... I'm enrolled in the Computer Programmer Analyst program at my local college starting this fall and I've got a copy of the text book already so I am trying to read through it before classes begin. I'm brand new to the programming world so I know almost nothing.

    I'm going to post a picture of the page so you can see exactly how the question is posed. I'm stuck on the Checkpoint question #26 because I'm not sure how I'm supposed to write out the statement.

    I am fairly certain that I should be making variable "b" and "c" a float. But beyond that I'm confused.

    Any advice is much appreciated! Thank you in advance and sorry for the long post for such a small question.

    uploadfromtaptalk1407333378833.jpg


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Conversion between primitive data types: mixed mathematical expressions

    Please post the text of the question instead of a picture of your homework.

    Please post the example program you wrote (in the form of an MCVE) that tests your answer to the question.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member codingninja's Avatar
    Join Date
    Aug 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Ok..

    The variable "a" is a FLOAT and the variable "b" is a DOUBLE. How can I write a statement that will assign the value of "b" to "a" without causing an error when the program is compiled?

    My guess would be that it looks like this..

    totalPay = float
    basePay = 500
    bonus = 1000

    totalPay = (double) (basePay + bonus)

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Conversion between primitive data types: mixed mathematical expressions

    And what happened when you wrote an example program that tested that guess?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member codingninja's Avatar
    Join Date
    Aug 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Conversion between primitive data types: mixed mathematical expressions

    It's just giving me so many errors I don't even know where to start or how to explain.

    This is what I have in eclipse...

    package org.totalbeginner.tutorial;

    public class person {
    // practice

    public class PayCheque
    {
    public static void main(String[] args)
    {
    // variables to calculate total pay from base and bonus

    int totalPay = basePay + bonus;
    basePay = 500, bonus = 1000

    totalPay = float
    totalPay = (double)(basePay + bonus)

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Conversion between primitive data types: mixed mathematical expressions

    For future reference, you can use the highlight tags to preserve code formatting.

    Well, take those errors one at a time.

    Is this really your full code? You've got a lot going on. What is the person class doing? Why is the PayCheque class inside of it? It doesn't make sense to have an inner class that contains a main method.

    You're using the basePay and bonus variables before you declare them, and when you do declare them, you aren't giving them a type.

    totalPay = float doesn't make any syntactic sense. Is that supposed to be a declaration?

    I highly suggest going through the basic tutorials and comparing your syntax to what's in them: Trail: Learning the Java Language (The Java™ Tutorials)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    codingninja (August 7th, 2014)

  8. #7
    Junior Member codingninja's Avatar
    Join Date
    Aug 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Ok, I will. Thanks for the advice 😕

Similar Threads

  1. Why Java Data types called primitive?
    By pedong in forum Java Theory & Questions
    Replies: 11
    Last Post: October 3rd, 2013, 10:20 PM
  2. Is .equals() redefined for primitive types?
    By Andrew R in forum Java Theory & Questions
    Replies: 1
    Last Post: September 11th, 2013, 11:05 AM
  3. Tutorial: Primitive Data Types
    By newbie in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 5th, 2012, 11:56 PM
  4. Primitive types
    By sudesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 30th, 2012, 01:44 AM
  5. [SOLVED] Converting primitive data types causing NumberFormatException
    By Melawe in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 10th, 2011, 12:30 AM