Write a program that will first ask for two values:
If any of the two values is negative, inform the user of this, and do nothing further. If however, the numbers are positive, then calculate and print out the amount of hours worked per month.
Sample output if values are negative:
You have entered a negative number
Sample output if values are O.K.:
You have worked 156 hours this month
Write a program that will ask the user to input a string. Store this string in a String
variable and print out the following:
"Hello"
in itSample output:
The string you input does not have the word "Hello" in it.
The string you input has a space at the end
This code will calculate the total hours worked appropriately:
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the number of days worked in a week");
int daysWorked = keyboard.nextInt();
System.out.println("Please enter the number of hours worked per day");
int hoursWorked = keyboard.nextInt();
if (daysWorked < 0 || hoursWorked < 0) {
System.out.println("You entered a negative number. Good bye!");
} else {
int totalWorked = daysWorked * hoursWorked * 30; // assuming 30 days in a month
System.out.println("Total hours worked in the month: " + totalWorked);
}
This code will process the input string appropriately:
System.out.println("Please enter a string");
String inputString = keyboard.nextLine();
int positionOfSubString = inputString.indexOf("hello");
if (positionOfSubString != -1) {
System.out.println("The string you entered has a \"hello\" in it ");
} else {
System.out.println("The string you entered doesn't have a \"hello\" in it ");
}
int lastCharPosition = inputString.length() - 1;
char lastChar = inputString.charAt(lastCharPosition);
if(lastChar == ' ') {
System.out.println("The last character in your string is a space");
} else {
System.out.println("The last character in your string is not a space");
}