Commit e832acbb authored by Qazi Zain's avatar Qazi Zain

new task are added

parent 1770bb2c
package JavaTrainingTask.Question5;
import java.util.Scanner;
public class Patterns {
int no;
int row;
Scanner input;
Patterns()
{
System.out.println("Object is Created!");
no=0;
row=0;
input= new Scanner(System.in);
}
void printPyramid(int rows)
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows- i - 1; j++) { // let suppose row is 4-i(0)-1 = 3rd col space print
System.out.print(" "); // 4-i(1)-1= 2
}
for (int k = 0; k < 2 * i + 1; k++) { // let suppose k which is 2*i(0)+1= 1( one time star will print
System.out.print("*"); // 2*i(1)+1= 3 star will print.
}
System.out.println();
}
}
void printGparamid()
{
System.out.println("Enter no :");
no= input.nextInt();
for(int i=no;i>0;i--)
System.out.println("Enter no of row:");
row= input.nextInt();
for (int i = 0; i < row; i++)
{
for(int j=i;j>0;j--)
for (int j = 0; j < i; j++)
{
System.out.print(" ");
}
for (int k = 0; k < (2 * (row - i) - 1); k++)
{
System.out.print("*");
}
System.out.println("");
System.out.println();
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < row - i - 1; j++) { // let suppose row is 4-i(0)-1 = 3rd col space print
System.out.print(" "); // 4-i(1)-1= 2
}
for (int k = 0; k < 2 * i + 1; k++) { // let suppose k which is 2*i(0)+1= 1( one time star will print
System.out.print("*"); // 2*i(1)+1= 3 star will print.
}
System.out.println();
}
}
}
void printTogalingDimond()
{
System.out.println("Enter no of row:");
row= input.nextInt();
for (int i = 0; i < row; i++) {
for (int j = 0; j < row - i - 1; j++) { // let suppose row is 4-i(0)-1 = 3rd col space print
System.out.print(" "); // 4-i(1)-1= 2
}
for (int k = 0; k < 2 * i + 1; k++) { // let suppose k which is 2*i(0)+1= 1( one time star will print
System.out.print("*"); // 2*i(1)+1= 3 star will print.
}
System.out.println();
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < i; j++)
{
System.out.print(" ");
}
for (int k = 0; k < (2 * (row - i) - 1); k++)
{
System.out.print("*");
}
System.out.println();
}
}
void printVerticleTile()
{
System.out.println("Enter no of rows");
int rows= input.nextInt();
for(int i=0;i<rows;i++)
{
printPyramid(rows);
}
}
void printHorizontalTile()
{
System.out.println("Enter no of rows");
int rows= input.nextInt();
for(int i=0;i<1;i++)
{
for(int j=0;j<3;j++) {
printPyramid(rows);
}
}
}
}
......@@ -4,6 +4,12 @@ public class main {
public static void main(String[] args) {
Patterns obj = new Patterns();
obj.printGparamid();
//obj.printGparamid();
// obj.printTogalingDimond();
//obj.printVerticleTile();
obj.printHorizontalTile();
}
}
package JavaTrainingTask.Question8;
public class Matrix {
char [][]arr;
Matrix()
{
System.out.println("Object is created!");
arr = new char[3][3];
}
void printArray()
{
arr=new char[][]{{'a','b','c'},{'d','e','f'},{'g','h','i'}};
for(int i=0;i< arr.length;i++)
{
for(int j=0;j< arr.length;j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
void swapColumn() {
// Ensure the array has at least two columns to swap
if (arr[0].length < 2) {
System.out.println("Not enough columns to swap.");
return;
}
// Swap the first column (index 0) with the last column (index arr[0].length - 1)
for (int i = 0; i < arr.length; i++)
{
char temp = arr[i][0];
arr[i][0] = arr[i][arr[i].length - 1];
arr[i][arr[i].length - 1] = temp;
}
System.out.println("Array after swapping first and last columns:");
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
package JavaTrainingTask.Question8;
public class main {
public static void main(String[] args) {
Matrix obj = new Matrix();
obj.printArray();
obj.swapColumn();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment