Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Eclipse project named [ your pitt id]_hw3 Create a package edu.pitt.is1

ID: 639442 • Letter: C

Question

Create a Eclipse project named [your pitt id]_hw3

Create a package edu.pitt.is17.[your pitt id].hw3

For each of the following exercises, create a class ExerciseX (where X is the exercise letter, for example ExerciseA) and implement the program and/or method requested.

Write a program that ask the user for a number between 1 and 12 and prints the multiplication table of the input number. For example if the user enters the number 4, the program output will look like:

4x1=4

4x2=8

4 x 3 = 12

4 x 4 = 16

4 x 5 = 20

4 x 6 = 24

4 x 7 = 28

4 x 8 = 32

4 x 9 = 36

4 x 10 = 40

4 x 11 = 44

4 x 12 = 48

The program should keep asking the user to input a number if the user until the user inputs a valid number (1-12).

Implement a method that verify if a number is prime number. A prime number is an integer number that does not have factors (other numbers that exactly divide the number) other than 1 and itself. The method signature and return type is: public static boolean isPrime(int n)

Write a method that count how many punctuation marks are in a String. The method definition is: public static int countPunctuation()

For this problem, the punctuation mark symbols are: . , ; : ! ?

Write a method that receives a String and returns a char randomly selected within the string. The method is defined as: public static char selectAChar(String s)

A random number can be generated using Math.random(), which returns a random number (double) within 0 and 1 (1 not included). To get a random number between 0 and n (excluding n), you can do:

(int)(Math.random() * n)

E. Rot 13. Implement the method public static String rot13(String s)

Rot 13 is a simple cipher algorithm that takes advantage
of the ASCII code associated to each character. Consider
that character letter 'A' (uppercase) has the code 65, 'B'
is 66, 'C' is 67, and so on until 'Z' which have the code 90
(see the ASCII table below). Rot 13 simply adds 13 to the
code of each letter, and if the sum goes beyond the 'Z'
(90), then continues on the 'A' (65). For example to
cipher the letter 'R' (82), it sums 13 going beyond 'Z' by 5
and sums 5 to 'A', resulting in 'F'. (82 + 13 = 95, 'Z' is 90,
so continues counting 5 more from 'A'). Since here are 26
letters in the alphabet, the cipher approach is completely reversible. As you can see in the figure at the side, the letters 'A' to 'M' have a mapping to the letters 'N' to 'Z' and vice versa. If this explanation is still confusing, please read the Wikipedia article: http://en.wikipedia.org/wiki/ROT13

You can implement the method by summing 13 (and continue after 'Z' starting form 'A') or by mapping characters as shown in the figure. Your choice.

Convert the string to uppercase. You do not need to deal with both uppercase and lowercase letters. Include lowercase if you want.

(source http://www.commfront.com/ascii-chart-table.htm)

Additionnally, consider correcting the indentation of your code (in Eclipse, select all code and then go to menu Source - Correct Indentation).

Explanation / Answer

//The java program that prompts for a number
//and prints a table
package edu.pitt.is17your_pitt_id;
import java.util.Scanner;
public class ExerciseA
{
   public static void main(String[] args)
   {
       Scanner reader=new Scanner(System.in);
      
       System.out.println("Enter a number (1-12)");
       int number=reader.nextInt();
      
      
       while(number>0 &&number<13)
       {
           for (int i = 1; i <=12; i++)
           {
               System.out.printf("%-2d x %-2d = %-4d ",number,i,number*i);
           }
           System.out.println("Enter a number (1-12)");
           number=reader.nextInt();
       }
      
       System.out.println("Invalid Number");
      
   }
}
Sample output:
Enter a number (1-12)
2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
2 x 11 = 22
2 x 12 = 24
Enter a number (1-12)
-1
Invalid Number
---------------------------------------------------------------------

//The java program that promts for a number
//and prints whether the given number is prime
//not prime
package edu.pitt.is17your_pitt_id;
import java.util.Scanner;
public class ExerciseB
{
   public static void main(String[] args)
   {
Scanner reader=new Scanner(System.in);
      
       System.out.println("Enter a number ");
       int number=reader.nextInt();
      
       if(isPrime(number))
           System.out.println(number + " is Prime Number");
       else
           System.out.println(number + " is not Prime Number");
   }
  
   public static boolean isPrime(int n)
   {
       boolean flag=true;
       for(int i=2;i<=n/2;i++)
       {
           int res=n%i;
           if(res==0)
           {
               flag=false;
               break;
           }
       }
      
       return flag;
   }
}

Sample output:
Enter a number
11
11 is Prime Number

------------------------------------------------------------------------------------


//Java program that counts the number of punctuation marks
//in the given text
package edu.pitt.is17your_pitt_id;
public class ExerciseC
{
   public static void main(String[] args)
   {
       String text="Hello.World ! java ends with;how ? ";

       System.out.println(text);
       int count=countPunctuation(text);
       System.out.println("Punctuation Count :"+count);
   }

   public static int countPunctuation(String text)
   {
       int puntctuationCount=0;
       for (int i = 0; i < text.length(); i++)
       {
           if(text.charAt(i)=='.')
               puntctuationCount++;
           else if(text.charAt(i)==',')
               puntctuationCount++;
           else if(text.charAt(i)==';')
               puntctuationCount++;
           else if(text.charAt(i)==':')
               puntctuationCount++;
           else if(text.charAt(i)=='!')
               puntctuationCount++;
           else if(text.charAt(i)=='?')
               puntctuationCount++;

       }
       return puntctuationCount;

   }
}

Sample output:

Hello.World ! java ends with;how ?
Punctuation Count :4
-------------------------------------------------------------------------------------


//The java program to demonstrte the selectAChar
//method that returns the random character from
//the string.
package edu.pitt.is17your_pitt_id;
public class ExerciseD
{
   public static void main(String[] args)
   {
       String text="HelloWorld";
       System.out.println(text);
       char ch=selectAChar(text);
       System.out.println("Random character:");
       System.out.println(ch);
   }
  
   public static char selectAChar(String s)
   {
       int length=s.length();
       int random=(int)(Math.random()*length);
       return s.charAt(random);
      
   }
}

Sample output:

HelloWorld
Random character:
l

-----------------------------------------------------------------------------


/*Java program that converst the text in upper case
* to the rot13 cipher text
* */
package edu.pitt.is17your_pitt_id;
public class EerciseE
{
   public static void main(String[] args)
   {
       String text="RAJ";
       System.out.println("Text Message :"+text);
       text=text.toUpperCase();
       String rot13=rot13(text);
       System.out.println("Rot13 : "+rot13);
   }
  
   public static String rot13(String s)
   {
       int b;
       String newmessage="";
      
       //for each character
       for(int i=0;i<s.length();i++)          
       {
           //for upper case letter
           if(Character.isUpperCase(s.charAt(i)))
           {
              
               //shift by 13
               b=(int)s.charAt(i)+13;                            
               if(b>'Z')
               {//and wrap if necessary
                   b='A'+(char)(b-'Z');
               }
              
               newmessage+=(char)b;
           }

       }
       return newmessage;

   }

}
Text Message :RAJ
Rot13 : FNW

Hope this helps you