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 12 of 12

Thread: What is cast operator and how to use it?

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default What is cast operator and how to use it?

    1. Write a program that prompts the user to input an integer between 0 and 35. If the number is less then or equal to 9, the program should output the number; otherwise, it should output A for 10, B for 11, C for 12, ..., and Z for 35. (Hint: Us the cast operator, (char)(), for numbers >=10.

    Sample output:

    The value entered is 12 and the output is C
    The valus entered is 5 and the output is 5

    2) Write a program to compute gross pay. The inputs to your algorithm are the hours worked per week and the hourly pay rate. The rule for determining gross pay is to pay the regular pay rate for all hours worked up to 40, time-and-half for all hours worked over 40 up to 54, and double time forall hours over 54. Compute and display the value for gross pay using this rule. Your program should output hours worked, pay rate, and gross pay. Format your output to display two decimal places.

    OK, I will post what I have after I get off work, also so that its clear Im not asking you to do it for me, just for pointers, hints or even and explanation
    Could someone explain the cast operator and how to use it please?
    Last edited by napenthia; April 24th, 2009 at 10:51 PM. Reason: spelling errors


  2. #2
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: Need A Little Homework Help Please

    Dont mean to be rude but could you show us what you have done first. We don't want to do your work for you.

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Need A Little Homework Help Please

    and im not asking you to do my work for if i were i would stright out say that, all im asking for are some pointers and if i need some help or have questions then ill ask them, im not so ignorant as not to try and do it myself or at least try to learn, if you dont want to help thats fine by me anyway what i have thus far

    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
            int number;
     
            System.out.println("Enter anumber between 0 and 35 ");
            number = scan.nextInt();
            if (number<=9){
                System.out.println(number);
     
            }
     
        }
     
    }
    Last edited by JavaPF; April 27th, 2009 at 03:18 AM. Reason: Please add the code tags..

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need A Little Homework Help Please

    Please use code tags

    the cast operator works like this
    (char)(65)

    65 is the ascii value for 'A', not you cannot straight cast the number the user inputs if it is above 10, this is because the first value of for the ascii chart is 65, so you would neeed to add (65-10) so 55 to the number the user entered, before casting it.
    (char)(number+55)

    The second question we will adress once you have finished he first

    Thanks,
    Chris

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Need A Little Homework Help Please

    ok so based off of what you told me about so far this is what i was able to come up with but when i run it i dont get the letter for the output of the number am i missing something. im wondering if im putting in the wrong way for the char

       import java.util.Scanner;
     
    public class Main {
     
            public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
            int number;
     
            System.out.println("Enter anumber between 0 and 35 ");
            number = scan.nextInt();
     
            if (number<=9){
                System.out.println("The number you entered is "+ number +" " +
                        "and the output is " + (char)(number+55));
                          }

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need A Little Homework Help Please

    if (number<=9){
    System.out.println("The number you entered is "+ number +" " +
    "and the output is " + (char)(number+55));
    }
    that is saying if the number is less than or equal to 9 print its characters representation. You should be doing it when the number is greater than 9 and less than or equal to 35.

    Chris

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

    napenthia (April 26th, 2009)

  8. #7
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Need A Little Homework Help Please

    oh ok I got i fixed it, can you check it for me and tell me if i need to fix anything, also when i try to get the number for numbers 1 though 9 to output the number the number wont come up so i was wondering if i missed something somewhere

     public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
            int number;
     
            System.out.println("Enter a number between 0 and 35 ");
            number = scan.nextInt();
     
            if (number<=35){
                System.out.println("The number you entered is "+ number +" " +
                        "and the output is " + (char)(number+55));
                          }
            else if (number<=9){
                System.out.println("The number you entered is " + number+ "and " +
                        "the output is " + number);
            }
    Last edited by napenthia; April 26th, 2009 at 04:39 AM.

  9. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need A Little Homework Help Please

    It's because your first if also includes numbers less than or equal to 9. So they get converted into chars also.

    Chris

  10. The Following User Says Thank You to Freaky Chris For This Useful Post:

    napenthia (April 27th, 2009)

  11. #9
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Need A Little Homework Help Please

    ok i got it now im gonna start on the next one, but what I want to know is should I be using the if, else statement for this one as well?
    also how do I use the decimal places?
    Last edited by napenthia; April 26th, 2009 at 04:55 PM.

  12. #10
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Need A Little Homework Help Please

    Quote Originally Posted by Freaky Chris View Post
    It's because your first if also includes numbers less than or equal to 9. So they get converted into chars also.

    Chris
    thanks for all you help I think I got the hang of it

  13. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need A Little Homework Help Please

    Hello napenthia,

    We answered question 2 for someone else recently. It sounds exactly the same.. I'll see if I can find the thread.

    You should check out our Tips & Tutorials forum. There are lots of useful code snippets in there including this which will help you:

    http://www.javaprogrammingforums.com...al-places.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  14. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need A Little Homework Help Please

    There is a similar pay calculator thread here:

    http://www.javaprogrammingforums.com...wo-errors.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM