Thursday, 1 June 2017

JAVA Tutorial-2

  1. Write a program to display a random choice from a set of six choices for breakfast (you could use any set; for example, scrambled eggs, waffles, fruit, cereal, toast, or yogurt).
  2. When testing whether an integer is a prime, it is sufficient to try to divide by integers up to the square root of the number being tested. Write a program to use this approach.
  3. A lottery requires that you select six different numbers from the integers 1 to 49. Write a program to do this for you and generate five sets of entries.
  4. Write a program to generate a random sequence of capital letters that does not include vowels.
  5. Write an interactive program to print a string entrered in a pyramid form. For instance, the string “stream” has to be displayed as follows:
    S
    S t
    S t r
    S t r e
    S t r e a
    S t r e a m
  6. Write an interactive program to print a diamond shape. For example, if user enters the number 3, the diamond will be as follows:
        *
       * *
      * * *
       * *
        *
       ...
    

1. Write a program to display a random choice from a set of six choices for breakfast (you could use any set; for example, scrambled eggs, waffles, fruit, cereal, toast, or yogurt).

 
import java.util.Random;
class choice
{
   public static void main(String args[])
   {
	Random randomGenerator = new Random();
	int r;
	r=randomGenerator.nextInt(6);
	System.out.println("random number="+r);
	System.out.println("1.scrambled eggs");
	System.out.println("2.waffles");
	System.out.println("3.fruit");
	System.out.println("4.cereal");
	System.out.println("5.toast");
	System.out.println("6.yogurt");
	switch(r)
	{
		case 1:
		  System.out.print("Your Choiced Value Is= scrambled eggs");
		break;
		case 2:
		  System.out.print("Your Choiced Value Is= waffles");
		break;
		case 3:
		  System.out.print("Your Choiced Value Is= furit");
		break;
		case 4:
		  System.out.print("Your Choiced Value Is= cereal");
		break;
		case 5:
		  System.out.print("Your Choiced Value Is= toast");
		break;
		case 6:
		  System.out.print("Your Choiced Value Is= Yogurt");
		break;
		default:
		  System.out.println("Invalid Choice");
		break;
	}
   }
}

Output

2. When testing whether an integer is a prime, it is sufficient to try to divide by integers up to the square root of the number being tested. Write a program to use this approach.

 
import java.util.Scanner;
class primeno
{
   public static void main(String ar[])
   {
	int n,sq,f=0,i;
	Scanner s=new Scanner(System.in);
	System.out.print("Enter a number to find wheather this number is prime number or not: ");
	n=s.nextInt();
	sq=(int)(Math.sqrt(n));
	for(i=sq;i<n;i++)
	{
		if((n%i)==0)
		{
			f=1;
			break;
		}
	}
	if(f==1)
	{
		System.out.println(+n+" is not prime number");
	}
	else
	{
		System.out.println(+n+" is prime number");
	}
   }
}

Output

3. A lottery requires that you select six different numbers from the integers 1 to 49. Write a program to do this for you and generate five sets of entries.

 
class lottery
{
   public static void main(String ar[])
   {
	int lucky,i,f=0;
	int a[]=new int[6];
	System.out.println("Ticket No");
	for(i=0;i<6;i++)
	{
		a[i]=(int)((6*Math.random())+1);
	}
	lucky=(int)((49*Math.random())+1);
	for(i=0;i<6;i++)
	{
		System.out.println(+a[i]);
	}	
	System.out.println("Lucky Number: "+lucky);
	for(i=0;i<6;i++)
	{
		if(a[i]==lucky)
		{
			f=1;
			break;
		}
	}
	if(f==1)
	{
		System.out.println(+lucky+" no ticket is winner");	
	}
	else
	{
		System.out.println("Sorry today no one is winner");
	}
   }
}

Output

4. Write a program to generate a random sequence of capital letters that does not include vowels.

 
import java.util.Random;
class vowels
{
   public static void main(String a[])
   {
	int i;
	Random r = new Random();
	char c[] = new char[5]; 
	for(i=0;i<5;i++)
	{
		c[i]=(char)(r.nextInt(26) + 'A');
	}
	for(i=0;i<5;i++)
	{ 
		switch(c[i])
		{
			case 'A':
			  System.out.println("Sorry this letter is vowel");
			break;
			case 'E':
			  System.out.println("Sorry this letter is vowel");
			break;
			case 'I':
			  System.out.println("Sorry this letter is vowel");
			break;
			case 'O':
			  System.out.println("Sorry this letter is vowel");
			break;
			case 'U':
			  System.out.println("Sorry this letter is vowel");
			break;
			default:
 			  System.out.println(c[i]);
   			break;
		}
	}
   }
}

Output

5. Write an interactive program to print a string entrered in a pyramid form. For instance, the string “stream” has to be displayed as follows:
S
S t
S t r
S t r e
S t r e a
S t r e a m

 
import java.io.*;
class pyramid
{
   public static void main(String a[])
   {
	char ch[]={'s','t','r','e','a','m'};
	String s=new String(ch);
	int i,j,k,n;
	n=s.length();
	for(i=n;i>0;i--)
	{
	        j=0;
		for(k=i;k<=n;k++)
		{
			System.out.print(ch[j]);
			System.out.print(" ");
			j++;
		}
		System.out.println("");
	}	
   }
}

Output

6. Write an interactive program to print a diamond shape. For example, if user enters the number 3, the diamond will be as follows:
    *
   * *
  * * *
   * *
    *
   ...

 
class diamond
{
   public static void main(String a[])
   {
	int i,j,k,n;
	n=Integer.parseInt(a[0]);
	for(i=n;i>0;i--)
	{
		for(j=0;j<i;j++)
		{
			System.out.print(" ");
		}
		j=0;
		for(k=i;k<=n;k++)
		{
			System.out.print("*");
			System.out.print(" ");
			j++;
		}
		System.out.println("");
	}
	for(i=n-1;i>0;i--)
	{
		for(j=i;j<=n;j++)
		{
			System.out.print(" ");
		}
		j=0;
		for(k=i;k>0;k--)
		{
			System.out.print("*");
			System.out.print(" ");
			j++;
		}
		System.out.println("");
	}
   }
}

Output

Share:

Related Posts:

  • JAVA Tutorial-3 Create an array of String variables and initialize the array with the names of the months from January to December. Create an array containing 12 random decimal values bet… Read More
  • JAVA Tutorial-4 Program 1 Program 2 Program 3 Program 4 Program 5 1. create a class called student having data member like name,enr,branch,city,spi define construct which can ini d… Read More
  • JAVA Tutorial-6 Program 1 Program 2 Program 3 Program 4 1. Write a program to insert values into an array. The user will enter a sequence of index and value to be stored at that i… Read More
  • JAVA Tutorial-5 Program 1 Program 2 Program 3 Program 4 Program 5 Program 6 1. The abstract Vegetable class has three subclasses named Potato, Brinjal and Tomato. Write an applica… Read More
  • JAVA Tutorial-2 Write a program to display a random choice from a set of six choices for breakfast (you could use any set; for example, scrambled eggs, waffles, fruit, cereal, toast, or … Read More

2 comments: