0% found this document useful (0 votes)
134 views16 pages

Java 123

The document contains code snippets from Java programs. The first program swaps the values of two integer variables a and b by performing arithmetic operations on them. The second program finds the maximum and minimum value from three integer variables using conditional operators. The third program prints all prime numbers between 1 to 100 by checking if a number is divisible only by 1 and itself.

Uploaded by

Kola Naresh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
134 views16 pages

Java 123

The document contains code snippets from Java programs. The first program swaps the values of two integer variables a and b by performing arithmetic operations on them. The second program finds the maximum and minimum value from three integer variables using conditional operators. The third program prints all prime numbers between 1 to 100 by checking if a number is divisible only by 1 and itself.

Uploaded by

Kola Naresh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 16

1.

package swap;

public class swap {


static int a=5;
static int b=6;

public static void main(String[] args)


{
a=a+b;
b=a-b;
a=a-b;
System.out.println("a="+a);
System.out.println("b="+b);
}

2.

package conditionaloperators;

public class conditionaloperators {


static int a=5;
static int b=6;
static int c=7;

public static void main(String[] args)


{
int max=(a>b ? (a>c ? a : c): (b>c ? b : c));
System.out.println("max="+max);
int min=(a<b ? (a<c ? a : c): (b<c ? b : c));
System.out.println("min="+min);
}

3.
package primenumber;

public class primenumber {

public static void main(String[] args) {


int i =0;
int num =0;
String primeNumbers = "";
for (i=1;i<=100;i++)
{
int c=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
c = c + 1;
}
if (c==2)
primeNumbers = primeNumbers + i + " ";
}
System.out.println("Prime numbers from 1 to 100 are :"+primeNumbers);
}
}

skilling2
----------
1.
package Session2;

public class Circle {


static double r=3.7;
public static void main(String[] args)
{
System.out.println("area of circle="+3.14*r*r);
System.out.println("perimeer of circle="+2*3.14*r*r);
}
}

2.
package Session2;

public class runner


{
static double time =45.30,distance=14;
public static void main(String[] args)
{
time=time/60;
distance=distance/1.6;
System.out.println("The average speed is="+distance/time);
}

}
3.
package Session2;
import java.util.Scanner;
public class Student
{
static long sno;
static int m1,m2,m3;
static {
Scanner sc=new Scanner(System.in);
sno=sc.nextLong();
m1=sc.nextInt();
m2=sc.nextInt();
m3=sc.nextInt();
sc.close();
}
public static void main(String[] args)
{
System.out.println("student sno is"+sno);
System.out.println("student total marks="+(m1+m2+m3));
System.out.println("student average marks"+(m1+m2+m3/3));

skillig3
---------
1.
package session3;
import java.util.Scanner;

public class savingamount


{
static int amount;
static double samount=0;
static
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the amount ");
amount=sc.nextInt();
sc.close();
}
public static void main(String[] args)
{
for(int i=0;i<6;i++)
{
samount=(amount+samount)*(1+0.00417);
}
System.out.println("the saving amount is "+samount);
}
}
2.
package session3;

import java.util.Scanner;

public class perimetertriangle


{
static int a,b,c;
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter edges of a triangle");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
s.close();
if((a+b>c)&&(b+c>a)&&(c+a>b))
{
System.out.println("the perimeter of the triangle is :"+(a+b+c));
}
else
System.out.println("the input is invalid");
}
}
3.
package session3;

import java.util.Scanner;

public class leapyear


{
static int y;
static boolean year(int y)
{
if(y%4==0)
return true;
else
return false;
}
public static void main(String[] args)
{
System.out.println("enter y");
Scanner s=new Scanner(System.in);
y=s.nextInt();
s.close();
if(year(y)==true)
System.out.println("True");
else
System.out.println("False");
}
}
skillig4
---------
1.
package session4;

import java.util.Scanner;

public class Sumofn


{
static int n,sum=0;
static
{
System.out.println("enter n");
Scanner s=new Scanner(System.in);
n=s.nextInt();
s.close();
}
static void sum1(int n)
{
for(int i=1;i<=n;i++)
sum=sum+i;
System.out.println("sum of "+n+" number is "+sum);
}
public static void main(String[] args)
{
sum1(n);
}
}
2.
package session4;
import java.util.Scanner;

public class Speed


{
public static void main(String[] args)
{
int n;
System.out.println("enter speed");
Scanner s=new Scanner(System.in);
n=s.nextInt();
s.close();
Speed1.type(n);
}
}
package session4;

public class Speed1


{
public static void type(int n)
{
if(n>=60)
System.out.println("over speed");
else if(n>=40&&n<=60)
System.out.println("average speed");
else
System.out.println("safe drive");
}
}

3.
package session4;
import session3.Divisibles4;
import java.util.Scanner;

public class Divisible


{
public static void main(String[] args)
{
int n;
System.out.println("enter number");
Scanner s=new Scanner(System.in);
n=s.nextInt();
s.close();
Divisibles4.type(n);
}
}

package session3;
public class Divisibles4
{
public static void type(int n)
{
if(n%3==0&&n%7==0)
System.out.println("the number is divisible by both 3 and 7");
else if(n%3!=0&&n%7!=0)
System.out.println("the number is neither divisible bye 3 nor
7");
else if(n%3==0||n%7==0)
System.out.println("the is divisble by one 3 or 7");
}
}

package skilling5;
public class Class
{
double r;
double setr(double R)
{
r=R;
return R;
}
double getcircumference()
{
return 2*3.14*r;
}
double getarea()
{
return 3.14*r*r;
}
}

package skilling5;
import java.util.Scanner;
public class Session5
{
public static void main(String[] args)
{
Class obj=new Class();
Scanner sc=new Scanner(System.in);
double r=sc.nextDouble();
obj.setr(r);
System.out.println("Circumference of the
circle:"+obj.getcircumference());
System.out.println("area of the circle:"+obj.getarea());
sc.close();
}
}

package skilling5;
import java.util.Scanner;
public class set1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
variable obj=new variable();
obj.setx(a);
obj.sety(b);
obj.setz(c);
sc.close();
obj.abc();
}
}

package skilling5;
public class variable
{
private int a,b,c;
void setx(int x)
{
a=x;
}
void sety(int y)
{
b=y;
}
void setz(int z)
{
c=z;
}
void abc()
{
System.out.println(a+"\n"+b+"\n"+c+"\n");
}
}

package skilling6;
public class Class
{
public static String name;
public static String setname(String name)
{
return name;
}
public static void main(String[] args)
{
System.out.println(setname("Harish"));
}
}

package skilling6;
public class Student
{
int rollno;
String name;
String city;
Student(int rollno, String name, String city)
{
this.rollno=rollno;
this.name=name;
this.city=city;
}
public String toString() {
return rollno+" "+name+" "+city;
}
public static void main(String args[])
{
Student s1=new Student(101,"Raj","lucknow");
Student s2=new Student(102,"Vijay","ghaziabad");
System.out.println(s1); //compiler writes here s1.toString()
System.out.println(s2); //compiler writes here s2.toString()
}
}

package skilling7;
import java.util.Scanner;
public class PetrolPurchase
{
String s,p;
int q;
double pr,pd;
public String getS()
{
return s;
}
public void setS(String s)
{
this.s = s;
}
public String getP()
{
return p;
}
public void setP(String p)
{
this.p = p;
}
public int getQ()
{
return q;
}
public void setQ(int q)
{
this.q = q;
}
public double getPr()
{
return pr;
}
public void setPr(double pr)
{
this.pr = pr;
}
public double getPd()
{
return pd;
}
public void setPd(double pd)
{
this.pd = pd;
}
public double getpurchase()
{
return (q*pr)-(pd/100);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
PetrolPurchase obj=new PetrolPurchase();
String s=sc.next();
String p=sc.next();
int q=sc.nextInt();
double pr=sc.nextDouble();
double pd=sc.nextDouble();
obj.setS(s);
obj.setP(p);
obj.setPd(pd);
obj.setQ(q);
obj.setPr(pr);
System.out.println("station location:"+s);
System.out.println("Petrol:"+p);
System.out.println("Price:"+obj.getpurchase());
sc.close();
}
}

package skilling7;
import java.util.Scanner;
public class Mytriangle
{
public static int side1,side2,side3;
public static int getSide1()
{
return side1;
}
public static int getSide2()
{
return side2;
}
public static int getSide3()
{
return side3;
}
public static void setSide1(int side1)
{
Mytriangle.side1 = side1;
}
public static void setSide2(int side2)
{
Mytriangle.side2 = side2;
}
public static void setSide3(int side3)
{
Mytriangle.side3 = side3;
}
public static void isValid(int s1,int s2,int s3)
{
if(s1>0&&s2>0&&s3>0)
area(s1,s2,s3);
else
System.out.println("invalid");
}
public static void area(int s1,int s2,int s3)
{
double s=(s1+s2+s3)/2;
System.out.println("the area of the triangle is : "+Math.sqrt(s*(s-
s1)*(s-s2)*(s-s3)));
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
side1=sc.nextInt();
side2=sc.nextInt();
side3=sc.nextInt();
sc.close();
isValid(side1,side2,side3);
}
}

package skilling7;
import java.util.Date;
public class Date1
{
public int date,day,year;
public int getDate()
{
return date;
}
public int getDay()
{
return day;
}
public int getYear()
{
return year;
}
public void setDate(int date)
{
this.date = date;
}
public void setDay(int day)
{
this.day = day;
}
public void setYear(int year)
{
this.year = year;
}
public static void displayDate()
{
Date d=new Date();
System.out.println(d.getMonth()+"/"+d.getDay()+"/"+d.getYear());
}
public static void main(String[] args)
{
displayDate();
}
}
package skilling7;
import java.util.Scanner;
public class Class1
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in .nextInt();
in.close();
String s=Integer.toString(n);
if (n == Integer.parseInt(s))
{
System.out.println("Good job");
}
else
System.out.println("Wrong answer.");
}
}

package skilling8;
import java.util.Scanner;
public class MethodOverload8_1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
float side=sc.nextFloat();
double radius=sc.nextDouble();
double result1=area(side);
double result2=area(radius);
sc.close();
System.out.println("the area of square is : "+result1+"\n"+"the area of
circle is : "+result2);
}
public static double area(float side)
{
double x=side;
return x*x;
}
public static double area(double radius)
{
double r=radius;
return 3.14*r*r;
}
}

package skilling8;
import java.util.Scanner;
public class PrintNumber8_2
{
public static double printn(double i)
{
return i;
}
public static int printn(int d)
{
return d;
}
public static float printn(float f)
{
return f;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter any double number : ");
double i=sc.nextDouble();
System.out.println("enter any int number : ");
int d=sc.nextInt();
System.out.println("enter any float number : ");
float f=sc.nextFloat();
sc.close();
double int1=printn(i);
int dou1=printn(d);
float flo1=printn(f);
System.out.println("integer number is : "+dou1+"\n"+"double number is :
"+int1+"\n"+"float nnumber is : "+flo1);
}

package skilling8;
import java.util.Scanner;
public class area
{
static double l,b,s;
public static void area(double l,double b)
{
System.out.println("the area of the rectangle is:"+(l+b));
}
public static void area(double s)
{
System.out.println("area of square is :"+(s*s));
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
l=sc.nextDouble();
b=sc.nextDouble();
s=sc.nextDouble();
sc.close();
area(l,b);
area(s);
}
}
package skilling8;
public class BankDetails
{
public String name,bank;
public long acnumber;
public void acdetails()
{
System.out.println("name :"+name);
System.out.println("bank :"+bank);
System.out.println("account number :"+acnumber);
}
}

package skilling8;
import java.util.Scanner;
public class Demo
{
public static void main(String[] args)
{
BankDetails obj[]=new BankDetails[2];
Scanner sc=new Scanner(System.in);
for(int i=0;i<2;i++)
{
obj[i]=new BankDetails();
obj[i].name=sc.next();
obj[i].bank=sc.next();
obj[i].acnumber=sc.nextLong();
}
System.out.println("enter account number");
long ac=sc.nextLong();
sc.close();
for(int i=0;i<2;i++)
{
if(obj[i].acnumber==ac)
obj[i].acdetails();
}
}
}

package skilling9;
import java.util.Scanner;
public class EndOfFile
{
public static void main(String[] args)
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT.
Your class should be named Solution.*/
Scanner sc=new Scanner(System.in);
for(int i=1;sc.hasNext();i++)
{
System.out.println(i+" "+sc.nextLine());
}
sc.close();
}
}
package skilling9;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class MyLogin


{
public static void main(String[] args)
{
JFrame frame=new JFrame("My Login Window");
frame.setSize(400, 600);
frame.setLocation(400, 70);

JTextField t1=new JTextField();


t1.setBounds(200, 100, 100, 30);

JPasswordField pass=new JPasswordField();


pass.setBounds(200,150 , 100, 30);
pass.setEchoChar('*');

JLabel l1=new JLabel("Name");


l1.setBounds(100, 100, 70, 30);

JLabel l2=new JLabel("Password");


l2.setBounds(100, 150, 70, 30);

JButton b1=new JButton("Submit");


b1.setBounds(100, 200, 90,40);

JButton b2=new JButton("Cancel");


b2.setBounds(200, 200, 90,40);

Panel p=new Panel();


p.setLayout(null);
p.add(t1);
p.add(pass);
p.add(l1);
p.add(l2);
p.add(b1);
p.add(b2);
frame.add(p);
frame.setVisible(true);

b1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//add button code goes here.
System.out.println(t1.getText());
System.out.println(pass.getPassword());
}
});
b2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//add button code goes here.
t1.setText(null);
pass.setText(null);
//System.exit(0);
}
});
}
}

package skilling10;
public class Employee
{
long empid;
String name;
int salary;
Employee()
{

}
Employee(long id,String n,int s)
{
empid=id;
name=n;
salary=s;
}
public String toString()
{
return "empid="+empid+" name="+name+" salary="+salary;
}
public static void main(String[] args)
{
Employee e1=new Employee(1,"harish",100);
Employee e2=new Employee(2,"ravi",200);
Employee e3=new Employee(1,"ram",300);
System.out.println(e1);
System.out.println(e2);
System.out.println(e3);
}
//- overrides java.lang.Object.finalize
protected void finalize()
{

}
}
package skilling10;
public class Vehicle
{
int wheels;
String color;
Vehicle()
{
wheels=2;
color="White";
}
Vehicle(int w,String c)
{
wheels=w;
color=c;
}
public static void main(String[] args)
{
Vehicle v1=new Vehicle();
Vehicle v2=new Vehicle(4,"Blue");
System.out.println("wheels="+v1.wheels+" color="+v1.color);
System.out.println("wheels="+v2.wheels+" color="+v2.color);
}
}

You might also like