java patterns set
Post on 19-Feb-2018
228 Views
Preview:
TRANSCRIPT
-
7/23/2019 Java Patterns Set
1/16
Patterns Set
Q1. Write a program that asks the user to enter a character (ch) and an integer (n), and print the
character entered n times on the same line.
Example:
[15 marks]
A1.
import java.util.*;
class Q0101
{
public static void main(String[] args)
{
// user input
Scanner sc = new Scanner( System.in );
char ch;
int n;
String temp;
System.out.print("Enter a character and an integer: ");
ch = sc.next().charAt(0);
n = sc.nextInt();
temp = sc.nextLine(); // for the trailing newline
// print the patternint i;
for(i=1; i
-
7/23/2019 Java Patterns Set
2/16
import java.util.*;
class Q0102
{
public static void main(String[] args)
{
// user input
Scanner sc = new Scanner( System.in );
int n;
String temp;
System.out.print("Enter an integer: ");
n = sc.nextInt();
temp = sc.nextLine(); // for the trailing newline
// print the pattern
int i;
char ch = 'A';
for(i=1; i
-
7/23/2019 Java Patterns Set
3/16
int n;
String temp;
System.out.print("Enter an integer: ");
n = sc.nextInt();
temp = sc.nextLine(); // for the trailing newline
// print the pattern
int i, j;
char ch = 'A';
for(i=1; i
-
7/23/2019 Java Patterns Set
4/16
A4.
import java.util.*;
class Q0104
{
public static void main(String[] args)
{// user input
Scanner sc = new Scanner( System.in );
char ch;
int n;
String temp;
System.out.print("Enter a character and an integer: ");
ch = sc.next().charAt(0);
n = sc.nextInt();
temp = sc.nextLine(); // for the trailing newline
// print the pattern
int i, j;
char c; // running character to print
for(i=1; i
-
7/23/2019 Java Patterns Set
5/16
[15 marks]
A5.
import java.util.*;
class Q0105
{
public static void main(String[] args)
{
// user inputScanner sc = new Scanner( System.in );
char ch;
int n;
String temp;
System.out.print("Enter a character and an integer: ");
ch = sc.next().charAt(0);
n = sc.nextInt();
temp = sc.nextLine(); // for the trailing newline
// print the pattern
int i, j;
for(i=1; i
-
7/23/2019 Java Patterns Set
6/16
Sample output:
Q6. Write a program that asks the user to enter a character (ch), and the integer length and the
breadth values. Print an open rectangle having size length and breadth using character ch.
[15 marks]
A6.
import java.util.*;
class Q0106
{
public static void main(String[] args)
{// user input
Scanner sc = new Scanner( System.in );
char ch;
int length, breadth;
String temp;
System.out.print("Enter a character: ");
ch = sc.nextLine().charAt(0);
System.out.print("Enter length and breadth: ");
length = sc.nextInt();
breadth = sc.nextInt();
temp = sc.nextLine(); // for the trailing newline
// print the patternint i, j;
for(i=1; i
-
7/23/2019 Java Patterns Set
7/16
{
printChar(ch, 1);
printChar(' ', breadth-2);
printChar(ch, 1);
System.out.println();
}
}
}
// utility method to print ch, width times
public static void printChar(char ch, int width)
{
int i;
for(i=1; i
-
7/23/2019 Java Patterns Set
8/16
Scanner sc = new Scanner( System.in );
char ch;
int n = 0;
String temp;
System.out.print("Enter a character: ");
ch = sc.nextLine().charAt(0);
while( (n%2) != 1 ) // until an odd number is entered
{
System.out.print("Enter an odd integer: ");
n = sc.nextInt();
temp = sc.nextLine(); // for the trailing newline
if( (n%2) != 1 )
System.out.println("Enter an odd integer only. Try again!");
}
// print the pattern
int i, j;
int centerLine = (n+1)/2;
for(i=1; i
-
7/23/2019 Java Patterns Set
9/16
Q8. Write a menu-driven program that prints:
i) An unfilled triangle: ask the number of rows and the character to use for printing
ii) A filled triangle: ask the number of rows and the character to use for printing
A8.
import java.util.*;
class Q0108
{
public static void main(String[] args)
{
char menuOption = ' '; // space
do
{
http://www.computerapplicationsicse.com/wp-content/uploads/2013/09/Pattern0108.pnghttp://www.computerapplicationsicse.com/wp-content/uploads/2013/09/Pattern0107.pnghttp://www.computerapplicationsicse.com/wp-content/uploads/2013/09/Pattern0108.pnghttp://www.computerapplicationsicse.com/wp-content/uploads/2013/09/Pattern0107.png -
7/23/2019 Java Patterns Set
10/16
menuOption = askMenuOption();
switch(menuOption)
{
case 'U':
doUnfilledTriangle();
break;
case 'F':
doFilledTriangle();
break;
case 'X':
System.out.println("Exiting program. Thank you!");
break;
}
}while( menuOption != 'X' );
}
// ask user to enter menu option
// if wrong option entered, ask again
public static char askMenuOption()
{
char option = ' '; // space
do{
option = askChar("Enter U for Unfilled, F for Filled, X to Exit:
");
option = Character.toUpperCase(option);
if("UFX".indexOf(option) < 0)
System.out.println("Incorrect option. Try again...");
}while("UFX".indexOf(option) < 0);
return option;
}
// utility method to display the prompt
// and ask to enter a character
// return the character entered
public static char askChar(String prompt)
{
Scanner sc = new Scanner( System.in );
System.out.print(prompt);
char ch = sc.nextLine().charAt(0);
return ch;
}
// utility method to display the prompt
// and ask to enter an integer
// return the int entered
public static int askInt(String prompt)
{
Scanner sc = new Scanner( System.in );System.out.print(prompt);
int n = sc.nextInt();
String temp = sc.nextLine(); // for the trailing newline
return n;
}
// utility method to print a char ch, n times
public static void printChar(char ch, int n)
{
-
7/23/2019 Java Patterns Set
11/16
int i;
for(i=1; i
-
7/23/2019 Java Patterns Set
12/16
Q9. Write a program that asks the user to enter an integer (n) and print the following pattern as
shown in the example below:
A9.
import java.util.*;
class Q0109
{
public static void main(String[] args)
{
// user inputScanner sc = new Scanner( System.in );
System.out.print("Enter an integer: ");
int n = sc.nextInt();
String temp = sc.nextLine(); // for the trailing newline
// print the pattern
char ch = 'A';
int i, j;
for(i=1; i
-
7/23/2019 Java Patterns Set
13/16
{
for(j=1; j
-
7/23/2019 Java Patterns Set
14/16
int i, j;
for(i=n; i>=1; i--)
{
for(j=1; j
-
7/23/2019 Java Patterns Set
15/16
for(i=n; i>=1; i--)
{
ch = 'A'; // each line starts with 'A'
for(j=1; j
-
7/23/2019 Java Patterns Set
16/16
int i, j;
for(i=n; i>=1; i--)
{
ch = (char)('A' + n - 1); // each line starts with 'A'
for(j=1; j
top related