Вы находитесь на странице: 1из 17

Dr.

AMBEDKAR INSTITUTE OF TECHNOLOGY


(An Autonomous Institution, Aided by Government of Karnataka, Affiliated to VTU, Belgaum)

Near Janana Bharathi Campus, Mallathahalli, Bangalore-560056

JAVA & J2EE ASSIGNMENT

Department of Computer Science and Engineering

2019-20

Submitted by:
Ekta Singh
1DA17CS046
5th Semester
 Write a program which will print a pyramid of ‘*’ of
height ‘n’ and print the number of ‘*’ in the pyramid.

import java.util.Scanner;
public class Program1 {
static Scanner scan = new Scanner(System.in);

void Print()
{
System.out.print("Enter the height ");
int rows = scan.nextInt();
int k = 0;
int count = 0;
for(int i = 1; i <= rows; ++i, k = 0)
{
for(int space = 1; space <= rows - i; ++space)
{
System.out.print(" ");
}
while(k != 2 * i - 1)
{
System.out.print("* ");
++k;
count = count + 1;
}
System.out.println();
}
System.out.println("Total number of * are: "+ count);

}
public static void main(String[] args) {
Program1 p1 = new Program1();
p1.Print();
}
}

OUTPUT:
 Write a program which with print a pascal pyramid of *
of height.
import java.util.Scanner;

public class Pattern2

static Scanner scan = new Scanner(System.in);

public static void printTriagle(int n)

for (int i=0; i<n; i++)

for (int j=n-i; j>1; j--)

System.out.print(" ");

}
for (int j=0; j<=i; j++ )

System.out.print("* ");

System.out.println();

public static void main(String args[])

System.out.print("Enter the Height to print the triangle: ");

int n = scan.nextInt();

printTriagle(n);

OUTPUT:
 Write a program to print Symmetric Pascal’s Triangle of
‘*’ of height of odd length of input ‘1’ and even then
your program will print “Invalid Line Number”

import java.util.Scanner;
public class Pattern
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

if(rows%2==0)
System.out.println("Invalid Line Number");
else {
int cal = (rows/2)+1;

System.out.println("Here is your pattern");

for (int i= 0; i<= cal ; i++)


{
for (int j=cal; j>=i; j--)
{
System.out.print(" ");
}
for (int k=0; k<=i-1; k++)
{
System.out.print("*" + " ");
}
System.out.println("");
}

for (int i= 0; i<= cal-1 ; i++)


{
for (int j=-1; j<=i; j++)
{
System.out.print(" ");
}
for (int k=0; k<=cal-2-i; k++)
{
System.out.print("*" + " ");
}
System.out.println("");
}

}
sc.close();
}
}

OUTPUT:
 Write a Program to display any digit(n) from 0-9. Write
“7 Segment Display”.

import java.lang.*;
import java.io.*;
import java.util.Scanner;
class PrintSevenSegment
{
void Print1(char c)
{
if(c=='2'||c=='3'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9'||c=='0')
System.out.print(" _");
else
System.out.print(" ");
}

void Print2(char c)
{
if(c=='4'||c=='5'||c=='6'||c=='8'||c=='9'||c=='0')
System.out.print("|");
else
System.out.print(" ");
}

void Print3(char c)
{
if(c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='8'||c=='9')
System.out.print("_");
else
System.out.print(" ");
}

void Print4(char c)
{
if(c=='1'||c=='2'||c=='3'||c=='4'||c=='7'||c=='8'||c=='9'||c=='0')
System.out.print("|");
else
System.out.print(" ");
}

void Print5(char c)
{
if(c=='2'||c=='6'||c=='8'||c=='0')
System.out.print("|");
else
System.out.print(" ");
}

void Print6(char c)
{
if(c=='2'||c=='3'||c=='5'||c=='6'||c=='8'||c=='9'||c=='0')
System.out.print("_");
else
System.out.print(" ");
}

void Print7(char c)
{
if(c=='1'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9'||c=='0')
System.out.print("|");
else
System.out.print(" ");
}
public static void main(String[] args)
{

PrintSevenSegment obj =new PrintSevenSegment();


String number;

Scanner sc = new Scanner(System.in);


System.out.println("Enter a number:");
number = sc.nextLine();

for(int i=0; i<number.length();i++)


{
obj.Print1(number.charAt(i));
if (i!=number.length()-1)
{
System.out.print(" ");
}
}
System.out.println();
for(int i=0; i<number.length();i++)
{
obj.Print2(number.charAt(i));
obj.Print3(number.charAt(i));
obj.Print4(number.charAt(i));
}
System.out.println();

for(int i=0; i<number.length();i++)


{

obj.Print5(number.charAt(i));
obj.Print6(number.charAt(i));
obj.Print7(number.charAt(i));
}

OUTPUT:

 Write an Applet Program to show the animation of


Bouncing ball.
import java.applet.*;
import java.awt.*;

public class BouncingBall extends Applet implements Runnable


{
int x = 150, y = 50, r=20;
int dx = 11, dy = 7;
Thread t;
boolean stopFlag;
public void start()
{
t = new Thread(this);
stopFlag=false;
t.start();
}

public void paint(Graphics g)


{
g.setColor(Color.red);
g.fillOval(x-r, y-r, r*2, r*2);
}

public void run()


{
while(true)
{
if(stopFlag)
break;
if ((x - r + dx < 0) || (x + r + dx > bounds().width)) dx = -dx;
if ((y - r + dy < 0) || (y + r + dy > bounds().height)) dy = -dy;
x += dx; y += dy;

try
{
Thread.sleep(100);
}
catch(Exception e)
{
System.out.println(e);
};
repaint();
}
}

public void stop()


{
stopFlag=true;
t=null;
}
}
OUTPUT:

 Write an Applet Program for the Simple Calculator.


import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="Cal" width=300 height=300>

</applet>
*/

public class Calculator extends Applet

implements ActionListener

String msg=" ";

int v1,v2,result;

TextField t1;

Button b[]=new Button[10];

Button add,sub,mul,div,clear,mod,EQ;

char OP;

public void init()

Color k=new Color(120,89,90);

setBackground(k);

t1=new TextField(10);

GridLayout gl=new GridLayout(4,5);

setLayout(gl);

for(int i=0;i<10;i++)

b[i]=new Button(""+i);

add=new Button("add");

sub=new Button("sub");

mul=new Button("mul");

div=new Button("div");

mod=new Button("mod");

clear=new Button("clear");

EQ=new Button("EQ");

t1.addActionListener(this);

add(t1);
for(int i=0;i<10;i++)

add(b[i]);

add(add);

add(sub);

add(mul);

add(div);

add(mod);

add(clear);

add(EQ);

for(int i=0;i<10;i++)

b[i].addActionListener(this);

add.addActionListener(this);

sub.addActionListener(this);

mul.addActionListener(this);

div.addActionListener(this);

mod.addActionListener(this);

clear.addActionListener(this);

EQ.addActionListener(this);

public void actionPerformed(ActionEvent ae)

String str=ae.getActionCommand();

char ch=str.charAt(0);

if ( Character.isDigit(ch))

t1.setText(t1.getText()+str);

else
if(str.equals("add"))

v1=Integer.parseInt(t1.getText());

OP='+';

t1.setText("");

else if(str.equals("sub"))

v1=Integer.parseInt(t1.getText());

OP='-';

t1.setText("");

else if(str.equals("mul"))

v1=Integer.parseInt(t1.getText());

OP='*';

t1.setText("");

else if(str.equals("div"))

v1=Integer.parseInt(t1.getText());

OP='/';

t1.setText("");

else if(str.equals("mod"))

v1=Integer.parseInt(t1.getText());

OP='%';

t1.setText("");

if(str.equals("EQ"))
{

v2=Integer.parseInt(t1.getText());

if(OP=='+')

result=v1+v2;

else if(OP=='-')

result=v1-v2;

else if(OP=='*')

result=v1*v2;

else if(OP=='/')

result=v1/v2;

else if(OP=='%')

result=v1%v2;

t1.setText(""+result);

if(str.equals("clear"))

t1.setText("");

OUTPUT:

Вам также может понравиться