# What is cast operator and how to use it?

• April 24th, 2009, 08:27 PM
napenthia
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?
• April 24th, 2009, 09:53 PM
Fendaril
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.:cool:
• April 24th, 2009, 10:43 PM
napenthia
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

Code :

```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);   }   }   }```
• April 25th, 2009, 12:54 AM
Freaky Chris
Re: Need A Little Homework Help Please

the cast operator works like this
Code :

`(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.
Code :

`(char)(number+55)`

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

Thanks,
Chris
• April 25th, 2009, 06:33 PM
napenthia
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

Code :

``` 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)); }```
• April 26th, 2009, 01:29 AM
Freaky Chris
Re: Need A Little Homework Help Please
Code :

```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
• April 26th, 2009, 04:28 AM
napenthia
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

Code :

``` 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); }```
• April 26th, 2009, 01:17 PM
Freaky Chris
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
• April 26th, 2009, 04:49 PM
napenthia
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?
• April 27th, 2009, 03:19 AM
napenthia
Re: Need A Little Homework Help Please
Quote:

Originally Posted by Freaky Chris
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
• April 27th, 2009, 05:57 AM
JavaPF
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
• April 27th, 2009, 06:11 AM
JavaPF
Re: Need A Little Homework Help Please
There is a similar pay calculator thread here:

http://www.javaprogrammingforums.com...wo-errors.html