Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*------------------------------------------------------
- My name: Maysar Mahawi
- My subject code: DPIT 111
- My student number: 6548684
- My email address: msmm828@uowmail.edu.au
- Assignment number: 01
- -------------------------------------------------------*/
- package question2; // package and it's name.
- import java.util.Scanner; // imports the scanner class.
- public class question2 { // class question2 (question 2 is what the file is called)
- public static char convert(String string, int number) { // a static method of type char called convert, stores a string and an int.
- char MoveOneLetterForwards = string.charAt(number-1); // "char moveOneLetterForwards" is initializing a character of the name up, charAt(number here) finds the character of the given string at that index given
- int asciiValue = (int ) MoveOneLetterForwards; // convert char to ascii value
- asciiValue = asciiValue -32; // the difference is -32
- char upperCaseCharacter = (char)asciiValue; // convert ascii value to char
- return upperCaseCharacter; // store/returns "upperCaseCharacter" into asciiValue.
- }
- public static void main(String[] args) { // main method, main () is the starting point for every single java program. The main function differs from any other cuntion due to the fact that it auomatially executes whatever is inside of it
- Scanner input = new Scanner(System.in); // create a scanner object, allows users to write information for example numbers and letters.
- // input1
- System.out.print("Enter a string: "); // outputs "Enter x: ", and with the help of the scanner object people can also input.
- String yourName = input.nextLine().toLowerCase(); // read user String, using the variable String as well as letting the computer know that the input will be a String.
- // input2
- System.out.print("enter an index: "); // outputs "enter an index: " and with the help of the scanner object, users can input.
- int index = input.nextInt()+1; // the +1 is to go one number forward since that first index is actually 0.
- while (index < 0) { // checks that inputed number is greater than 0 meaning that code will continue only with numbers greater than zero. (though users can input 0, the line down below will +1).
- System.out.println("enter a positive index: "); // if number is negative, it will print the line down below.
- index = input.nextInt()+1; // prints out only if the user did not enter a positive number at the beginning.
- }
- System.out.println("The uppercase of " + index + " letter of " + yourName + " is " + convert(yourName, index)); // prints out whatever is in between the " " + the string the user inputs + the index the user inputs + calls convert method and putting the string and int inputs inside.
- }
- }
- /* extra information:
- * exact copy of exercise is not possible, we have not been taught some of the stuff!!!!
- * output: "The uppercase of 5th letter of your name is N"
- * unless 4 is a fixed value, it's not possible to put the "th" in because we can't say 1th or 2th
- * Something of this sort would be possible but I wasn't sure wether to go for it or not:
- * https://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-ordinal
- *
- * Had the value 4 been fixed, code would be:
- * System.out.println("The uppercase of 5th letter of " + yourName + " is " + convert(yourName, index));
- *
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement