0% found this document useful (0 votes)
220 views

FILE

The document contains 13 code snippets that demonstrate various Java programming concepts like: 1. Checking if two numbers are equal or finding the greater number 2. Calculating sum, average, and percentage of marks for 3 subjects 3. Accepting command line arguments and displaying them in a formatted output The code snippets cover concepts such as conditionals, loops, methods, method overloading, constructors, parsing command line arguments, and sorting values. Each snippet is accompanied by its corresponding output to demonstrate the working of the program.

Uploaded by

Divya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views

FILE

The document contains 13 code snippets that demonstrate various Java programming concepts like: 1. Checking if two numbers are equal or finding the greater number 2. Calculating sum, average, and percentage of marks for 3 subjects 3. Accepting command line arguments and displaying them in a formatted output The code snippets cover concepts such as conditionals, loops, methods, method overloading, constructors, parsing command line arguments, and sorting values. Each snippet is accompanied by its corresponding output to demonstrate the working of the program.

Uploaded by

Divya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1.Write a program to check whether the given two numbers in the program are equal.

If not, then
find the greater of the given two numbers

Code:

class First{
public static void main(String args[])
{
int a=10,b=5;
if(a==b)
{
System.out.println("a and b are equal numbers ");
}
else{
if(a>b)
System.out.println("a is greater than b");
else
System.out.println("b is gerater than a");
}
}
}

Output:

--------------------------------------------------------------------------------------------------------------------------------------

2.Write a program to find sum, average and percentage of Marks of three subjects of a Student.

Code:

class First{

public static void main(String args[])


{
float sub1=77,sub2=90,sub3=80,sum,avg,per;

sum=sub1+sub2+sub3;
avg=sum/3;
per=avg;
System.out.println("Sum of 3 Subjects = "+sum);
System.out.println("Average of 3 Subjects = "+avg);
System.out.println("Percentage = "+per);

}
}
Output

--------------------------------------------------------------------------------------------------------------------------------------

3. Write a program to check whether the given 2 numbers in the programs are equal .If not then
find the greater of 2 given numbers.

public class large


{
public static void main(String args[])
{
int a=15,b=12,c=21;
if(a>b && a>c)
System.out.println(+a+" is larger than "+c+" and "+b);
else if(b>a && b>c)
System.out.println(+b+" is larger than "+c+" and "+a);
else
System.out.println(+c+" is larger than "+a+" and "+b);
}
}

OUTPUT:

4.Write a program to find the factorial of a number 10.

class fact
{
public static void main(String [] args)
{
int a=10,i,fact;
fact=1;
for (i=1;i<=10;i++)
{
fact=fact*i;
}
System.out.println("factorial of 10="+fact);
}
}

OUTPUT :

--------------------------------------------------------------------------------------------------------------------------------------

5. Write a program to find the cube of number between 1 to 10.

class Cube
{
public static void main(String [] args)
{
int i;
for(i=1;i<=10;i++)
{
cube=i*i*i;
System.out.println("cube of "+i+"="+i*i*i);
}
}
}

OUTPUT

--------------------------------------------------------------------------------------------------------------------------------------
6 Write a program to print the following the pattern

class table
{
public static void main(String [] args)
{
int i;
for(i=0;i<10;i++)
{
System.out.println("\n");
for(int j=0;j<=i;j++)
{
System.out.print(i*j+"\t");
}
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------

7. Write a Java program that has a method for the calculation of the fourth power of 2.

class Fourth{

public static void main(String args[])


{
int p=1;
for(int i=1;i<=4;i++){
p=2*p;
}
System.out.println("Fourth power of 2="+p);
}
}
Output:
8. Write a Java program that has a method for initialization of variables to 10 and 20 and another
method which will display the same.

Code:

class MethodA{
int x,y;
public void setVar(int a,int b){
x=a;
y=b;
}
public void display(){
System.out.println("b="+x+" a="+y);
}
}

class VarSet{
public static void main(String args[])
{
MethodA a1=new MethodA();
a1.setVar(10,20);
a1.display();
}
}

OUTPUT:

--------------------------------------------------------------------------------------------------------------------------------------

9. Write a Java program using the concept of Method Overloading to add two integers, add three
integers, add two double type values and to concatenate two strings.

Code

class over{
int res;
void add(int a,int b){
res=a+b;
System.out.println("Sum of (4,5) integres="+res);
}
void add(int a,int b ,int c){
res=a+b+c;
System.out.println("Sum of (4,5,3) intergers="+res);
}

void add(double a,double b){


double res=a+b;
System.out.println("Sum of (3.5,4.0) doubles="+res);
}

void add(String a,String b){


String res=a+b;
System.out.println("String Concatenation="+res);
}

public static void main(String args[]){


over o1=new over();
o1.add(4,5);
o1.add(4,5,3);
o1.add(3.5,4.0);
o1.add("Today is "," Monday");
}
}

Output

----------------------------------------------------------------------------------------------------------------
10. Write a class Rectangle with overloaded constructors. In the first constructor accept only one
parameter and the code for the constructor should display that “It is a Square” and calculate the
area. The second constructor should take two parameters and display “It is a Rectangle” and
calculate the area.

Code

class Rectangle{
Rectangle(double side){
System.out.println("It is a Square with side="+side+" and Area ="+side*side);
}

Rectangle(double length ,double breadth){


System.out.println("It is a Rectangle with Length ="+length+"and
breadth="+breadth);
System.out.println("It's Area="+length*breadth);
}

public static void main(String args[]){


Rectangle r1=new Rectangle(4);
Rectangle r2=new Rectangle(4,5);
}
}

Output:

--------------------------------------------------------------------------------------------------------------------------------------

11. Write a program to accept various features of java as command line arguments and
display the total number of arguments with their features in the following format :-

No. of arguments:
1 : Java is Simple
2 : Java is Object-oriented
3 : Java is Distributed
4 : Java is secure
5 : Java is Multithreaded

Program

class javaFeatures
{
public static void main(String args[])
{
String s1=args[0],s2=args[1],s3=args[2],s4=args[3],s5=args[4];
int count=0;
System.out.println("Number of arguments :");
for(int i=0;i<args.length;i++){
count=i+1;
System.out.println(count+" : "+" Java is "+args[i]);
}
}
}
----------------------------------------------------------------------------------------------------------------

12. Write a java program to accept parameters on the command line .If there are no
command line argument entered, the program should print an error message and exit
else the arguments should be printed.

printArg.java

class printArg
{
public static void main(String args[])
{
int count=0;
if(args.length>0)
{
for(int i=0;i<args.length;i++)
{
count=i+1;
System.out.println("Argument "+count+" : "+args[i]);
}
}
else{
System.out.println("No Arguments Entered");
}
}
}
OUTPUT

----------------------------------------------------------------------------------------------------------------

13. Write a java program to accept 3 command line arguments from the user and
display it in sorted order .

class sortArg
{
public static void main(String args[])
{
int num1,num2,num3,temp=0;
num1=Integer.parseInt(args[0]);
num2=Integer.parseInt(args[1]);
num3=Integer.parseInt(args[2]);
if ((num1 > num2 && num1 > num3))
{
if(num2 > num3)
{
System.out.print(num3 + " " + num2 + " " + num1);
}
else
System.out.print(num2 + " " + num3 + " " + num1);
}
else if ((num2 > num1 && num2 > num3))
{
if(num1 > num3)
{
System.out.print(num3 + " " + num1 + " " + num1);
}
else
{
System.out.print(num1 + " " + num3 + " " + num2);
}
}
else if ((num3 > num1 && num3 > num2))
{
if(num1 > num2)
{
System.out.print(num2 + " " + num1 + " " + num3);
}
else
System.out.print(num1 + " " + num2 + " " + num3);
}
else
{
System.out.println("ERROR!");
}
}
}

Output

--------------------------------------------------------------------------------------------------------------------------------------

14.Write a program CompoundInterest.java that gets from its users a deposit amount, an interest
rate, and a term of deposit through command line arguments. The program then computes and
displays how the principal will change. The program should define and use a method
compoundAnnual() with three parameters: a starting amount, an interest rate represented as a
percentage, and the number of years. After acquiring these values, the program then computes
and displays how the principal will change. The formula for computing interest compounded on an
annual basis is as follows:

StartingAmount(1 + InterestRate/100)Years

15. Write a program to demonstrate the inheritance.

class W{
float var;
}

class X extends W{
String Xvar;
}

class Y extends X{
String Yvar;
}

class Z extends Y{
int Zvar;
}

class XYZ{
public static void main(String args[]){
Z w=new Z();
w.Zvar=21;
System.out.println("Z class , Integer var="+w.Zvar);

w.Xvar=new String("Hi");
System.out.println("X class , String var="+w.Xvar);

w.var=4.5f;
System.out.println("W class , Double var="+w.var);

w.Yvar=new String("Hello");
System.out.println("Y class , String var="+w.Yvar);
}
}

Output:
16. Create a base class called Shape. It should contain two methods getCoord() and showCord()
to accept x and y coordinates and to display the same respectively.
Code
class Shape{
int x_coord,y_coord;
public void getCoord(int x,int y){
x_coord=x;
y_coord=y;
}

public void showCoord(){


System.out.println("x coordinates:"+x_coord);
System.out.println("y coordinates:"+y_coord);
}

public static void main(String args[]){


Shape s1=new Shape();
s1.getCoord(100,200);
s1.showCoord();
}
}

Output

----------------------------------------------------------------------------------------------------------------

17.In the main method, execute the showCoord() method of the Rect class by applying the
dynamic method dispatch concept.

18. Create a sub class called Rect. It should also contain a method to display the length and
breadth of the rectangle called showCoord().

19. Write a program that determines the price of a movie ticket. The program asks for the
customer's age and for the time on a 24-hour clock (where noon is 1200 and 4:30PM is 1630).
The normal adult ticket price is Rs. 150, however the adult matinee price is Rs. 100. Adults are
those over 12 years. The normal children's ticket price is Rs.75, however the children's matinee
price is Rs. 50. Get the information from the user through Command Line and then use nested
if statements to determine the ticket price. It is usually a good idea to separate the "information
gathering phase" (asking the user for age and time) from the "processing phase" of a program
(determining the ticket price).
Print the total price to be paid accordingly.
20. Bob's Discount Bolts charges the following prices:
 5 cents per bolt
 3 cents per nut
 1 cent per washer
Write a program that asks the user for the number of bolts, nuts, and washers in their purchase
and then calculates and prints out the total. As an added feature, the program checks the order.
A correct order must have at least as many nuts as bolts and at least twice as many washers as
blots, otherwise the order has an error. For an error the program writes out "Check the Order:
too few nuts" or "Check the Order: too few washers" as appropriate. Both error messages are
written if the order has both errors. If there are no errors the program writes out "Order is
OK." In all cases the total price in Rupees (of the specified number of items) is written out.
Number of bolts: 12 Number of nuts: 8 Number of washers: 24
Check the Order: too few nuts Total cost: 108
Use constants for the unit cost of each item.

21. Write a program that reads a sentence given by the user through Command Line and prints
it out with each word reversed and each sentence reversed based on user’s choice but with the
words and punctuation in the original order as Reverse Words and Reverse Sentence. For
Example -
Input: Go to the main menu. ReverseWords
oG ot eht niam unem.

22. Write an application that concatenates the following three Strings: “Event Handlers is dedicated”, “to
make your event” and “a most memorable one Madam.” Print each string separately and the
concatenated String. Also perform the following on the concatenated String –
 a) Identify the length of a String
 b) Identify the index of the character ‘e’ after leaving 3 characters
 c) Replace all small ‘a’ character with capital ‘A’
 d) Verify that the String is ending with “Madam”
 e) Extract the String “Madam” and Verify that it is a palindrome.
 f) Count the total number of Vowels with the frequency in the given String.

Code:

class exercise{
public static void main(String args[])
{
String s1,s2,s3,s4,s5,s6;

s1="Event Handlers is dedicated";


s2="to make your event";
s3="a most memorable one Madam.";
s4=s1+s2+s3;
System.out.println("Concatenated String = "+s4);
System.out.println("Concatenated String Length= "+s4.length());
System.out.println(" e character index after leaving 3 characters = "+s4.indexOf("e",3));
System.out.println("Uppercase letters A = "+s4.replace("a","A"));
System.out.println("String ends with Madam= "+s4.endsWith("Madam"));
s5=s4.substring(66,71);
s5.equals()
System.out.println();
}

23. Create an interface called VariableTest which contains a method called disp() and two
variables x, y which are integers and whose value is set as 10 and 20. Create a class called
VarIntTest which implements this interface. The disp() method should display a message “Inside
Interface – Variable Test and method disp”.

Write a method called display() within VarIntTest class which will print the value of x.

24. Create a program to generate numbers from 1 to 15 with three light weight threads, where the
first thread should generate numbers from 1 to 5, the second thread generate numbers from 6 to
10 and the last thread generates from 11 to 15 .

25. Write a Java program to demonstrate that as a high-priority thread executes, it will delay the
execution of all lower-priority threads.

26. Write a Java program that demonstrates a high-priority thread using sleep to give lower-
priority threads a chance to run.

27.Using awt design

Code

package awt;
import java.awt.*;

public class exercise1 {


public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f1=new Frame();
f1.setSize(600,500);
f1.setTitle("Result");
f1.setLayout(null);
//Set Label
Label l1=new Label("Enter first Number ");
Label l2=new Label("Enter second Number ");
Label l3=new Label("RESULT");
l1.setBounds(50,50,200,30);
l2.setBounds(50,100,200,30);
l3.setBounds(50,150,200,30);
f1.add(l1); f1.add(l2);f1.add(l3);

//Set TextField
TextField t1=new TextField();
TextField t2=new TextField();
TextField t3=new TextField();
t1.setBounds(300,50,200,30);
t2.setBounds(300,100,200,30);
t3.setBounds(300,150,200,30);
f1.add(t1);f1.add(t2);f1.add(t3);

//add buttons
Button b1=new Button("ADD");
Button b2=new Button("SUB");
Button b3=new Button("MUL");
Button b4=new Button("DIV");
Button b5=new Button("CLEAR");
Button b6=new Button("EXIT");

b1.setBounds(50,300,50,30);
b2.setBounds(170,300,50,30);
b3.setBounds(300,300,50,30);
b4.setBounds(450,300,50,30);
b5.setBounds(170,400,50,30);
b6.setBounds(300,400,50,30);

f1.add(b1);f1.add(b2);f1.add(b3);f1.add(b4);f1.add(b5);f1.add(b6);

f1.show();
}

28.
Code

package awt;
import java.awt.*;
import java.awt.event.*;
public class exercise2 extends Frame{
Label l1,l2,l3,l4;
TextField t1,t2,t4;
TextArea t3;
Button b1,b2,b3;

public exercise2(){
Frame f1=new Frame();
f1.setSize(600,500);
f1.setTitle("EMPLOYEE INFORMATION");
f1.setLayout(null);
//Set Label
l1=new Label("Name ");
l2=new Label("Age");
l3=new Label("Address");
l4=new Label("City");
l1.setBounds(50,50,200,30);
l2.setBounds(50,100,200,30);
l3.setBounds(50,150,200,30);
l4.setBounds(50,300,200,30);
f1.add(l1); f1.add(l2);f1.add(l3);f1.add(l4);

//Set TextField
t1=new TextField();
t2=new TextField();
t3=new TextArea(4,100); //Not accepting parameters?
t4=new TextField();
t1.setBounds(300,50,200,30);
t2.setBounds(300,100,200,30);
t3.setBounds(300,150,200,100);
t4.setBounds(300,300,200,30);
f1.add(t1);f1.add(t2);f1.add(t3);f1.add(t4);

//add buttons
b1=new Button("SAVE");
b2=new Button("CLEAR");
b3=new Button("EXIT");

b1.setBounds(100,400,70,30);
b2.setBounds(250,400,70,30);
b3.setBounds(400,400,70,30);
b3.setBackground(Color.CYAN);
b3.addActionListener(new MyActionListener());
f1.add(b1);f1.add(b2);f1.add(b3);
f1.show();
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent o){
System.exit(0);
}
}

public static void main(String[] args) {


// TODO Auto-generated method stub
exercise2 e1 = new exercise2();
}
}

29.

Code

package awt;
import java.awt.*;

public class exercise3 {


public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f1=new Frame();
f1.setSize(600,500);
f1.setTitle("CALCULATOR");
f1.setLayout(null);

//Set TextField
TextField t1=new TextField();
t1.setBounds(150,50,300,30);
f1.add(t1);

//add buttons 0-4


Button b1=new Button("0");
Button b2=new Button("1");
Button b3=new Button("2");
Button b4=new Button("3");
Button b5=new Button("4");
//SetBounds
b1.setBounds(150,80,60,30);
b2.setBounds(210,80,60,30);
b3.setBounds(270,80,60,30);
b4.setBounds(330,80,60,30);
b5.setBounds(390,80,60,30);

//add buttons 5-9


Button b6=new Button("5");
Button b7=new Button("6");
Button b8=new Button("7");
Button b9=new Button("8");
Button b10=new Button("9");
//SetBounds
b6.setBounds(150,110,60,30);
b7.setBounds(210,110,60,30);
b8.setBounds(270,110,60,30);
b9.setBounds(330,110,60,30);
b10.setBounds(390,110,60,30);

//add buttons Operators


Button b11=new Button(".");
Button b12=new Button("00");
Button b13=new Button("+");
Button b14=new Button("-");
Button b15=new Button("/");
//SetBounds
b11.setBounds(150,140,60,30);
b12.setBounds(210,140,60,30);
b13.setBounds(270,140,60,30);
b14.setBounds(330,140,60,30);
b15.setBounds(390,140,60,30);

//button 16-20
Button b16=new Button("*");
Button b17=new Button("=");
Button b18=new Button("ON");
Button b19=new Button("OFF");
Button b20=new Button("EXIT");
//add setBounds
b16.setBounds(150,170,60,30);
b17.setBounds(210,170,60,30);
b18.setBounds(270,170,60,30);
b19.setBounds(330,170,60,30);
b20.setBounds(390,170,60,30);

b19.setBackground(Color.RED);
b18.setBackground(Color.GREEN);
b20.setBackground(Color.white);
t1.setBackground(Color.CYAN);

//Add Buttons
f1.add(b1);f1.add(b2);f1.add(b3);f1.add(b4);f1.add(b5);//f1.add(b6);
f1.add(b6);f1.add(b7);f1.add(b8);f1.add(b9);f1.add(b10);
f1.add(b11);f1.add(b12);f1.add(b13);f1.add(b14);f1.add(b15);
f1.add(b16);f1.add(b17);f1.add(b18);f1.add(b19);f1.add(b20);
f1.show();
}
}
12.Write a program to add 2 numbers through command line arguments.

class cmdAdd
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]),b=Integer.parseInt(args[2]);
int res;
System.out.println("Multiplication ="+a*b);
if(args[1].equals("+")){
res=a+b;
System.out.println("Sum ="+res);
}
else if(args[1].equals("-")){
res=a-b;
System.out.println("Subtraction ="+res);
}
else if(args[1].equals("*")){
res=a*b;
System.out.println("Multiplication ="+res);
}
else if(args[1].equals("/")){
res=a/b;
System.out.println("Division ="+res);
}
else{
System.out.println("Operator not recognised ");
}
}
}
OUTPUT:

----------------------------------------------------------------------------------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy