Professional Documents
Culture Documents
Java
Java
{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Ex1.Addition of two numbers (direct method)
import java.util.Scanner;
public class abc{
public static void main(String[] args){
int a, b, sum;
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number: ");
a = s.nextInt();
System.out.println("Enter the second number: ");
b = s.nextInt();
sum = a + b;
System.out.println("Sum of the two numbers is: ");
System.out.println(sum);
}
}
Ex4.Addition of two numbers by taking values from user using buffered reader class.
import java.io.*;
class Addition {
int a, b, c;
a = Integer.parseInt(br.readLine());
b = Integer.parseInt(br.readLine());
c = a + b;
}
Ex4.Addition of two numbers by taking values from user using DataInputStream class.
import java.io.*;
class Main {
public static void main(String args[]) throws Exception {
int a, b, c;
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Enter the two numbers to add:");
a = Integer.parseInt(dis.readLine());
b = Integer.parseInt(dis.readLine());
c = a + b;
System.out.println("\nSum of two numbers:" + c);
}
}
Ex5.Using classes and methods
a) Write a JAVA program to display default value of all primitive data type of
JAVA.
b) Write a case study on public static void main(250 words).
c) Five Bikers Compete in a race such that they drive at a constant speed which
may or may
not be the same as the other. To qualify the race, the speed of a racer must be more
than the average speed of all 5 racers. Take as input the speed of each racer and
print back the speed of qualifying racers.
public class Main {
static boolean a;
static int b;
static long c;
static float d;
static char e;
static double f;
import java.io.*;
import java.util.*;
class Race
{
public static void main(String [] args)
{
int R[]=new int[20];
float avg;
int sum=0;
System.out.println("Enter Speed of 5 members");
Scanner sc=new Scanner(System.in);
for(int j=1;j<=5;j++)
{
R[j]=sc.nextInt();
sum+=R[j];
}
avg=sum/5;
for(int i=1;i<=5;i++)
{
if(avg<R[i])
{
System.out.println("QualifiedRaceris:"+i+"with value="+R[i]);
}
}
}
}