Advertisement
Guest User

question2

a guest
Apr 4th, 2020
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. /*------------------------------------------------------
  2. My name: Maysar Mahawi
  3. My subject code: DPIT 111
  4. My student number: 6548684
  5. My email address: msmm828@uowmail.edu.au
  6. Assignment number: 01
  7. -------------------------------------------------------*/
  8.  
  9.  
  10. package question2;          // package and it's name.
  11.  
  12. import java.util.Scanner;  // imports the scanner class.
  13.  
  14. public class question2 {     // class question2 (question 2 is what the file is called)
  15.  
  16.    
  17.     public static char convert(String string, int number) {      // a static method of type char called convert, stores a string and an int.
  18.        
  19.         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
  20.  
  21.      
  22.  
  23.         int asciiValue = (int ) MoveOneLetterForwards;   // convert char to ascii value
  24.         asciiValue = asciiValue -32;                      // the difference is -32
  25.         char upperCaseCharacter = (char)asciiValue;      // convert ascii value to char
  26.                
  27.         return upperCaseCharacter;                // store/returns "upperCaseCharacter" into asciiValue.
  28.     }
  29.    
  30.    
  31.     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
  32.  
  33.         Scanner input = new Scanner(System.in);   // create a scanner object, allows users to write information for example numbers and letters.
  34.        
  35. // input1
  36.         System.out.print("Enter a string: ");      // outputs "Enter x: ", and with the help of the scanner object people can also input.
  37.         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.
  38.        
  39. // input2
  40.         System.out.print("enter an index: ");     // outputs "enter an index: " and with the help of the scanner object, users can input.
  41.         int index = input.nextInt()+1;           // the +1 is to go one number forward since that first index is actually 0.
  42.         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).
  43.             System.out.println("enter a positive index: ");   // if number is negative, it will print the line down below.
  44.             index = input.nextInt()+1;                     // prints out only if the user did not enter a positive number at the beginning.
  45.         }
  46.        
  47.  
  48.  
  49.         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.
  50.  
  51.     }
  52.  
  53.    
  54. }
  55.  
  56. /* extra information:
  57. * exact copy of exercise is not possible, we have not been taught some of the stuff!!!!  
  58. * output: "The uppercase of 5th letter of your name is N"
  59. * unless 4 is a fixed value, it's not possible to put the "th" in because we can't say 1th or 2th
  60. * Something of this sort would be possible but I wasn't sure wether to go for it or not:
  61. * https://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-ordinal
  62. *
  63. * Had the value 4 been fixed, code would be:
  64. * System.out.println("The uppercase of 5th letter of " + yourName + " is " + convert(yourName, index));
  65. *
  66. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement