Documente Academic
Documente Profesional
Documente Cultură
Lab work#1
Design and implement an application that reads an integer value and prints the sum of all
the even integers between 2 and that.Print an error message if input value is less than 2.
Program:
importjava.util.Scanner;
public class fatima {
public static void main(String[] args)
{
int Number;
Scanner scan = new Scanner(System.in);
System.out.println("Enter an Integer greater than 1:");
Number = scan.nextInt();
intn,sum=0;
if(Number>2)
{
for(n=2;n<=Number;n=n+2)
{
sum+=n;
}
System.out.println("Sum of even numbers between 2 and " + Number + " inclusive is: " +sum);
}
else
{
System.out.println(“Error message” );
}
}}
Output:
Lab work# 2
Design and implement an application that reads a string from the user and prints it one
character per line.
Program:
importjava.util.Scanner;
public class fatima{
public static void main(String[] args) {
String phrase;
int marker=0,length;
char current;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string:");
phrase=scan.nextLine();
16-ELE-01 Page 55
Lab Report#9
length=phrase.length();
for (int count=0; count < length; count++){
current=phrase.charAt(marker);
System.out.println(current);
marker++;}
}}
Output:
Lab work# 3
Design an implement an application that produces a multiplication table showing result of
multiplication the integer through 1 12
program.
Public class fatima{
public static void main(String[] args) {
inti, s;
for (i = 1; i<= 12; i++){
System.out.print(i+ ":"+ "\t");
for (s= 1;s<=12;s++){
System.out.print(i*s+ ",");}
System.out.println();}}}
Output:
Lab work # 4
Design and implement an application that prints the first few verses of the travelling
song,Print each iteration one verse.
Program:
16-ELE-01 Page 56
Lab Report#9
Import java.util.Scanner;
public class fatima{
public static void main(String[] args) {
intstartbottles, endbottles,i;
String phrase1,phrase2,phrase3;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of bottles to start with:");
startbottles= scan.nextInt();
System.out.println("Enter the number of bottles to end on: ");
endbottles= scan.nextInt();
phrase1 = "bottles of beer on the wall" ;
phrase2 = "bottles of beer";
phrase3= "if one of those bottles should happen to fall";
if(startbottles>=1&&endbottles<=100){
for(i=startbottles; i>=endbottles; i--){
System.out.println(i + phrase1);
if(i<startbottles)
System.out.println();
if(i<startbottles&&i>endbottles)
System.out.println(i + phrase1);
if(i>endbottles)
System.out.println(i + phrase2);
if(i>endbottles)
System.out.println(phrase3);}}
else
System.out.println("wrong range");
}}}
Output:
Lab work# 5
Using Pairofdice class design an implement an application that rolls a pair of dice 1000
times counting the number of box cars that occurs.
Program:
Public class fatima {
public static void main(String[] args) {
finalint FLIPS = 100;
int Head=0, Tail=0;
Coin mycoin = new Coin();
for (inti = 0; i< FLIPS; i++){
mycoin.flip();
System.out.println (mycoin);
16-ELE-01 Page 57
Lab Report#9
if (mycoin.isHeads())
Head++;
else
Tail++;}
System.out.println("Heads :" + Head);
System.out.println("TAILS :" + Tail);}}
class Coin {
inti=0;
private final int HEADS = 0;
private final int TAILS = 1;
privateint face;
public Coin(){
flip();}
public void flip (){
face = (int) (Math.random() * 2);}
publicbooleanisHeads (){
return (face == HEADS);}
publicbooleanisTails (){
return (face == TAILS);}}
Lab work#6
Using coin class, design and implement a driver class called coin flip whose main method
flips a coin 100 times. Print the results.
Program:
Main class:
package coinflip;
public class coinflip {
public static void main(String[] args)
{final int FLIPS = 100;
int Head=0, Tail=0;
Coin mycoin = new Coin();
for (int i = 0; i < FLIPS; i++){
mycoin.flip();
System.out.println (mycoin);
if (mycoin.isHeads())
Head++;
else
Tail++;}
System.out.println("Heads :" + Head);
System.out.println("TAILS :" + Tail);}}
Sub class:
package coinflip;
public class Coin
{int i=0;
private final int HEADS = 0;
private final int TAILS = 1;
16-ELE-01 Page 58
Lab Report#9
C:
Program:
Class fatima {
public static void main(String[] args) {
inti, k, j; Output:
for(i=1;i<=10;i++)
for(k=0;k<=i;k++){
System.out.print(" ");}
for(j=10;j>=i;j--){
System.out.print("*");}
System.out.print("\n");}}}
D:
Program:
Class fatima {
public static void main(String[] args) {
inti, j, k;
for(i=1;i<=5;i++){
for(j=i;j<5;j++){
16-ELE-01 Page 59
Lab Report#9
System.out.print(" ");}
for(k=1;k<(i*2);k++){
System.out.print("*");}
System.out.print("\n");}
for(i=4;i>=1;i--){
for(j=5;j>i;j--){
System.out.print(" ");}
for(k=1;k<(i*2);k++){
System.out.print("*");}
System.out.print("\n");
}}}
Lab work # 8
Design and implement an application that prints a table showing a subset of the Unicode
charcter and numeric value……as they go across the row.
Program:
Public class fatima {
public static void main(String[] args) {
finalint PER_COLUMN = 5;
int count = 0, i;
for (i = 32; i<= 126; i += 1)
{
System.out.print (i + ": " + "\t"+ (char)i +"\t");
count++;
if (count % PER_COLUMN == 0) {
System.out.println();
}}}}
Output:
Lab work# 9
Design and implement an application that reads a string from the user then determine the
loswer case vowels.Having a separate counter for each vowel.
Program:
Import java.util.Scanner;
public class fatima{
16-ELE-01 Page 60
Lab Report#9
16-ELE-01 Page 61