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

java programming

The document contains a series of Java programming examples demonstrating various concepts such as temperature conversion, finding the largest number, calculating the area of shapes, and handling user input. Each example includes a program with an aim, code implementation, and expected output. The examples cover topics like arrays, classes, interfaces, and GUI components.

Uploaded by

yarudanee181513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java programming

The document contains a series of Java programming examples demonstrating various concepts such as temperature conversion, finding the largest number, calculating the area of shapes, and handling user input. Each example includes a program with an aim, code implementation, and expected output. The examples cover topics like arrays, classes, interfaces, and GUI components.

Uploaded by

yarudanee181513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

EX:1

Aim

Program

import java.lang.*;
import java.util.*;

class paul1a
{
public static void main(String args[])
{
System.out.println("Enter temperature in celcius");
Scanner s=new Scanner(System.in);
//String s1=s.nextLine();
int c=s.nextInt();
float f=c*9/5+32;

System.out.println("Fahrenheit value is"+f);


}
}

Output

Result:
EX:2
Aim

Program

import java.util.Scanner;
public class Ex2
{
public static void main(String args[])
{
int a,b,big;
Scanner scan=new Scanner(System.in);
System.out.println("Enter two numbers");
a=scan.nextInt();
b=scan.nextInt();
big=(a>b)?a:b;
System.out.println("Largest="+big);}}

Output

Result:
EX:3

Aim

Program

import java.util.Scanner;
import java.lang.*;
import java.io.*;
class Ex3
{
public static void main(String args[])
{
float area;
float r=Float.parseFloat(args[0]);
System.out.println("The radius of the circle is:"+r);
area=(float)22/7*r*r;
System.out.println("The area is:"+area);}}

Output:

Result:
EX:4

Aim

Program

import java.util.Scanner;
import java.lang.*;
import java.io.*;
class Ex4
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int marks[]=new int[5];
int sum=0;
double avg;
System.out.println("Enter your tenth std

marks one by one");


for(int i=0;i<5;i++){
System.out.println("Subject"+(i+1)+":");
marks[i]=s.nextInt();
sum+=marks[i];
}
avg=sum/5.0;
System.out.println("Total Marks"+sum);
System.out.println("Average Marks"+avg);}}
Result:

EX:5

Aim

Program

import java.util.Scanner;
import java.lang.*;
import java.io.*;
class Ex5
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
String names[]=new String[10];
System.out.println("Enter 10 student names");
for(int i=0;i<names.length;i++)
{
System.out.println("Name"+(i+1)+":");
names[i]=s.nextLine();
}

for(int i=0;i<names.length-1;i++){
for(int j=0;j<names.length-1-i;j++){
if(names[j].compareTo(names[j+1])>0){
String t=names[j];
names[j]=names[j+1];
names[j+1]=t;}}}

System.out.println("student names in

alphabetical order");
for(int i=0;i<names.length;i++)
{System.out.println(names[i]);}}}
Output

Result:
EX:6

Aim

Program

import java.util.Scanner;
class Student
{
String name;
String course;
String rollno;
int age;
Student(String n,int a,String rn,String c)
{
name=n;
age=a;
rollno=rn;
course=c;
}

void display()
{
System.out.println("Name :"+name);
System.out.println("Age :"+age);
System.out.println("Roll Number:"+rollno);
System.out.println("course :"+course);
}
}

public class Ex6a{


public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int i,n;
System.out.print("Enter the Number of Students:");
n=s.nextInt();
s.nextLine();
Student students[]=new Student[n];
for(i=0;i<n;i++)
{
System.out.println("Enter details For Students"+(i+1)+":");
System.out.println("Name:");
String name=s.nextLine();
System.out.print("Age:");
int age=s.nextInt();
s.nextLine();
System.out.print("Roll Number:");
String rollno=s.nextLine();
System.out.print("Course:");
String course=s.nextLine();
students[i]=new Student(name,age,rollno,course);
}
System.out.println("\nStudent Details:");
for(i=0;i<n;i++)
{
students[i].display();
System.out.println();
}}}

output
Result:
EX:7

Aim

Program

import java.util.Scanner;
class Area
{
double findArea(double length,double breadth)
{
return length*breadth;
}
public double findArea(double base,double height,boolean isTriangle)
{
return 0.5*base*height;
}
public double findArea(double side)
{
return side*side;
}
}

class Ex7
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
Area c=new Area();
double le,br,rect,ba,ht,tri,s,square;
System.out.println("Enter length and breadth of rectangle:");
le=scan.nextInt();
br=scan.nextInt();
rect=c.findArea(le,br);
System.out.println("Enter base and height of triangle:");
ba=scan.nextInt();
ht=scan.nextInt();
tri=c.findArea(ba,ht,true);
System.out.println("Enter side of square:");
s=scan.nextInt();
square=c.findArea(s);
System.out.println("Area of the rectangle:"+rect);
System.out.println("Area of the triangle:"+tri);
System.out.println("Area of the square:"+square);
}}

output

Result:
EX:8

Aim

Program

import java.util.Scanner;
class Shape
{
double getPerimeter()
{
return 0.0;
}
double getArea()
{
return 0.0;
}
}
class Circle extends Shape
{
double radius;
Circle(double r)
{
radius=r;
}
double getPerimeter()
{
return 2*22.0/7*radius;
}
double getArea()
{
return 22.0/7.0*radius*radius;
}}
class Ex8
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter Radius of Circle:");
double rad=scan.nextFloat();
Circle circle = new Circle(rad);
System.out.println("Perimeter of the circle:"+circle.getPerimeter());
System.out.println("Area of the circle:"+circle.getArea());
}
}
EX:9

Aim

Program

import java.util.Scanner;

interface Shape
{
double getArea();
}

class Rectangle implements Shape


{
double length;
double breadth;

Rectangle(double l,double b)
{
length=l;
breadth=b;
}

public double getArea()


{
return length*breadth;
}}

class Circle implements Shape


{
double radius;
Circle(double r)
{
radius=r;
}

public double getArea()


{
return 22.0/7.0*radius*radius;
}

}
class Triangle implements Shape
{
double base;
double height;
Triangle(double b,double h)
{
base=b;
height=h;
}

public double getArea()


{
return 0.5*base*height;
}
}

class Ex9
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter Length and bredth of Rectangle:");
double le=scan.nextDouble();
double br=scan.nextDouble();
Shape rectangle=new Rectangle(le,br);
System.out.println("Enter Radius of Circle :");
double rad=scan.nextDouble();
Shape circle=new Circle(rad);
System.out.println("Enter Base and Height of Triangle:");
double ba=scan.nextDouble();
double ht=scan.nextDouble();
Shape triangle=new Triangle(ba,ht);
System.out.println("Area of the Rectangle:"+rectangle.getArea());
System.out.println("Area of the Circle:"+circle.getArea());
System.out.println("Area of the Triangle:"+triangle.getArea());

}
}
EX:10

Aim

Program

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JEx10{
public static void main(String []args){
JFrame frame=new JFrame("color Changer");
frame.setSize(400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel pannel=new JPanel();
pannel.setLayout(new FlowLayout());

final JButton redButton=new JButton("Red");


final JButton blueButton=new JButton("Blue");
final JButton yellowButton=new JButton("Yellow");
pannel.add(redButton);
pannel.add(blueButton);
pannel.add(yellowButton);
ActionListener colorChange=new ActionListener(){

public void actionPerformed(ActionEvent e){


if(e.getSource()==redButton)
{
pannel.setBackground(Color.RED);
}
else if(e.getSource()==blueButton){
pannel.setBackground(Color.BLUE);
}
else if(e.getSource()==yellowButton){
pannel.setBackground(Color.YELLOW);
}
}
};
redButton.addActionListener(colorChange);
blueButton.addActionListener(colorChange);
yellowButton.addActionListener(colorChange);
frame.add(pannel);
frame.setVisible(true);
}
}

You might also like