Sunteți pe pagina 1din 22

class HelloWorld

{
public static void main(String arg[])
{
System.out.println("My First Java
Program!");
}
}

public class ArithmeticOperations {


public static void main(String agrs[]){
int x = 5;
int y = 10;
int result;
result = x + y;
}

System.out.println(result);

}
public class a
{
public static void main(String[] args)
{
double x = 4;
double y = Math.sqrt(x);
System.out.println(y);
}
}

double y = Math.pow(x, a);


Math.sin
Math.cos
Math.tan
Math.atan
Math.atan2
Math.exp
Math.log
Math.PI
Math.E
public class StringDemo {
public static void main(String agrs[]){
String greet = "HeLlO";
String name = "JaVa";
char SPACE = ' ';
greet = greet.toLowerCase();
name = name.toUpperCase();
String msg = greet + SPACE + name;
}

System.out.println(msg);

}
public class TypeConversion {
public static void main(String[] args) {
String sHeight = "65.43";
float h = Float.parseFloat(sHeight);
System.out.println(h);

public class BooleanDemo {


public static void main(String[] args) {
boolean b;
int weight = 40;
b = weight != 40;
System.out.println(b);
}

public class LogicalOperators {


public static void main(String args[]) {
boolean b;
int age = 17;
int weight = 32;
b = age < 15 || (age < 18 && weight <
40);
}

System.out.println(b);
}

class Person
{
public static void main(String args[]) {
int age=20;
if(age>21)

{
System.out.println("The person is Major");
}
else
{
System.out.println("The person is Minor");
}
}
}
class Student
{
public static void main(String args[])
{
//int testScore=70;
int testScore=55;

if(testScore<35)
{
System.out.println("C grade ");
}
else if((testScore>=35)&&(testScore<=60))
System.out.println("B grade");

}
else if((testScore>=60)&&(testScore<=75))
{
System.out.println("O grade");
}
else
{
System.out.println("A grade");

}
}

public class SwitchCaseDemo{


public static void main(String[] args){
int day = 3;
//int day=0;
//int day=-1;
//int day=15;
//int day=4;
String dName = "";
switch(day) {
case 0:
dName = "Sunday";
break;
case 1:
dName = "Monday";
break;
case 2:
dName = "Tuesday";
break;
case 3:
dName = "Wednesday";
break;
case 4:
dName = "Thursday";
//break;
case 5:
dName = "Friday";

break;
case 6:
dName = "Saturday";
break;
default:
dName = "Wrong Choice";
break;
}
System.out.println(dName);
}
}

public class ForloopDemo


{
public static void main(String[] args)
{
int i;
for(i=10;i<100;i=i+1)
{
if((i%3==0) || (i%5==0))
{
System.out.println(i);
}
}
}
}

public class WhileDemo {

public static void main(String[] args) {


int n = 13876;
int dSum = 0;
while (n > 0) {
dSum = dSum + (n % 10);
n = n / 10;
}
System.out.println(dSum);
}

public class ReverseNumber {


public static void main(String[] args) {
int n = 19435;
int rNum = 0;
while (n > 0) {
rNum = rNum * 10 + (n % 10);
n = n / 10;
}
System.out.println(rNum);
}
}

public class BinToDec {


public static void main(String[] args) {
int nBin = 11010;
int nDec = 0;
int Pow2 = 1;
while (nBin > 0) {

10));

nDec = nDec + (Pow2 * (nBin %


Pow2 = Pow2 * 2;
nBin = nBin / 10;
}
System.out.println(nDec);

public class DoWhileDemo {


public static void main(String[] args) {
int n = 0;
int x = 0;
do {
x = x + 1;
} while (x * x < n);
System.out.println(x * x == n);
}
}
class ArraysDemo
{
public static void main(String arg[])
{
int squares[] = new int[10];
int n, x;
for(x=0;x<10;x=x+1)
{
n=x+1;
squares[x]=n*n;
}
System.out.println(squares[2]);
}

public class Student

int roll_no;
String name;
void studentDetail() {
System.out.println("The roll number is" +
roll_no);
System.out.println("The name is" + name);
}
public static void main(String[] args)

System.out.println("We have created a class with


2 variables and 1 method");
}
}

class Student {

int roll_no;
String name;

public class TestStudent


{
public static void main(String args[])
{
Student stud1 = new Student();
Student stud2=new Student();
Student stud3=new Student();
//Student stud2=stud1;
stud1.roll_no=20;
stud1.name="Ramu";
stud2.roll_no=30;
stud2.name="Shyamu";
System.out.println("The roll number is" +
stud1.roll_no);
System.out.println("The name is " + stud1.name);

System.out.println("The roll number is" +


stud2.roll_no);
System.out.println("The name is" + stud2.name);
System.out.println("The roll number is" +
stud3.roll_no);
System.out.println("The name is " + stud3.name);
}
}

class Demo
{
public void show()
{
System.out.println("I am from other class");
}
}

public class MethodDemo


{

public static int displayMessage()


{
System.out.println("Hello Method");
//square(5);
return 7;
}
public void square(int a)
{
System.out.println(a*a);
}
public static void main(String arg[])
{
System.out.println(displayMessage());
Demo d = new Demo();
d.show();
}
}

public class Student {


int roll_number;

String name;
Student()
{
roll_number=10;
name="Raman";
}
void studentDetail()
{
System.out.println(roll_number);
System.out.println(name);
}
public static void main(String[] args) {
Student stu = new Student();
stu.studentDetail();
}
}

public class Student {


int roll_number;
String name;
Student(int roll_number,String name)
{
System.out.println("I am Parameterized
Constructor");
this.roll_number=roll_number;
this.name=name;

}
void studentDetail()
{
System.out.println(roll_number);
System.out.println(name);
}
public static void main(String[] args) {
Student stu3=new Student(11,"Raju");
stu3.studentDetail();
}
}

public class Student {


int roll_number;
String name;
Student()
{this(11);
System.out.println("I am Default
Constructor");
/* roll_number=0;
name=null;*/
}
Student(int r_no)
{this(11,"Raju");

System.out.println("I am a constructor
with a single parameter");
// roll_number=r_no;
}
Student(int roll_number,String name)
{
System.out.println("I am Parameterized
Constructor");

this.roll_number=roll_number;
this.name=name;
}
void studentDetail()
{
System.out.println(roll_number);
System.out.println(name);
}
public static void main(String[] args) {
Student stu = new Student();
stu.studentDetail();
/* Student stu2 = new Student(11);
stu2.studentDetail();
Student stu3=new Student(11,"Raju");
stu3.studentDetail();*/
}
}

class A {

int a ;
{
System.out.println("Non static block of an instance of
Class A");
System.out.println("The value of a is " + a);
}
{
System.out.println("Second Non static block of an
instance of Class A");
System.out.println("The value of a is " +a);
}
public A() {
System.out.println("Constructing object of type A");
System.out.println("The value of a is" + a);
}
}
public class NonStaticTest
{
public static void main(String[] args)
{

A a1= new A();


A a2=new A();
}
}

public class Student {


int roll_number;
String name;
Student(int number, String the_name)
{
roll_number=number;
name=the_name;
}
Student(int num)
{
roll_number=num;
name = "no name";
}
Student()
{
roll_number=0;
name="-";
}
Student(String the_name,int r_no)
{
roll_number=r_no;
name=the_name;
}
void studentDetail()

System.out.println(roll_number);
System.out.println(name);

}
public static void main(String[] args) {
Student s = new Student(22,"Ram");
s.studentDetail();
Student s2= new Student();
s2.studentDetail();
Student s3= new Student("Raju",45);
s3.studentDetail();
Student s4 = new Student(61);
s4.studentDetail();
}
}

public class Addition {


int a=10,b=5;
void add()
{
System.out.println(a+b);
}
void add(double num1, double num2)
{
System.out.println(num1 + num2);
}
void add(int n1, int n2, int n3)
{

System.out.println(n1+n2+n3);

}
void add(String s1,String s2)
{
System.out.println(s1+s2);
}
public static void main(String[] args) {
Addition obj = new Addition();
obj.add();
obj.add(2.5,3);
obj.add(1,5,4);
obj.add("Hello", " World");
}
}

import java.io.*;
public class InputBufferedReader
{
public static void main(String[] args) throws
IOException
{
String str,str1;
int n;
InputStreamReader isr = new
InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter your name");
str=br.readLine();
System.out.println("Enter your age");
str1=br.readLine();
n=Integer.parseInt(str1);

System.out.println("The name is " + str);


System.out.println("The age is " + n);
}

import java.io.*;
public class InputBufferedReader
{
public static void main(String[] args) throws
IOException
{
String str,str1;
int n;
// InputStreamReader isr = new
InputStreamReader(System.in);

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));
System.out.println("Enter your name");
str=br.readLine();
System.out.println("Enter your age");
str1=br.readLine();
n=Integer.parseInt(str1);
System.out.println("The name is " + str);
System.out.println("The age is " + n);
}
}

1. public class Welcome


2. {
3. public static void main(String[] args)
4. {
5. String[] greeting = new String[3];
6. greeting[0] = "Welcome to Core Java";
7. greeting[1] = "by Cay Horstmann";
8. greeting[2] = "and Gary Cornell";
9.
10. for (int i = 0; i < greeting.length; i++)
11. System.out.println(greeting[i]);
12. }
13. }

S-ar putea să vă placă și