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

Java Progrms

This document contains code snippets demonstrating various Java programming concepts: 1) A program to calculate the square root of a number using the Math class. 2) A program using different variable types and data types to store employee data. 3) Programs using different types of constructors to initialize student objects. 4) Programs demonstrating expression evaluation, autoboxing, unboxing, control statements like if-else, for loops, switch case etc. 5) Programs implementing OOPs concepts like encapsulation, inheritance and polymorphism. 6) Programs performing string manipulation operations.

Uploaded by

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

Java Progrms

This document contains code snippets demonstrating various Java programming concepts: 1) A program to calculate the square root of a number using the Math class. 2) A program using different variable types and data types to store employee data. 3) Programs using different types of constructors to initialize student objects. 4) Programs demonstrating expression evaluation, autoboxing, unboxing, control statements like if-else, for loops, switch case etc. 5) Programs implementing OOPs concepts like encapsulation, inheritance and polymorphism. 6) Programs performing string manipulation operations.

Uploaded by

Vijay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

Write a java program to perform simple squareroot:

Package squareroot;
Public class squareroot
{
public static void main(String[] args) {
double x = 64;
double y;
y=Math.sqrt(x);
System.out.println("y="+y);

}
2.Code execute and debug that uses different types of variables And data
types:

package variables;

public class employee {


int id;String name;float salary;
void insert(int i,String n,float s)
{
void display()
{
System.out.println(id+""+name+""+salary);
}

}
package variables;

public class emp {

public static void main(String[] args) {


// TODO Auto-generated method stub
employee e1=new employee();
employee e2=new employee();
employee e3=new employee();
e1.insert(101, "sudha", 450000);
e2.insert(102, "sudhi", 250000);
e3.insert(103, "sudhu", 150000);
e1.display();
e2.display();
e3.display();
}

}
3 (A)code ,execute and debug programs that uses different types of
constructors

package student;

public class student


{
String name;
int regno;
int marks1,marks2,marks3;
student()
{
name="raju";
regno=12345;
marks1=45;
marks2=76;
marks3=64;
}
student(String n,int r,int m1,int m2,int m3)
{
name=n;
regno=r;
marks1=m1;
marks2=m2;
marks3=m3;
}
student(student s)
{
name=s.name;
regno=s.regno;
marks1=s.marks1;
marks2=s.marks2;
marks3=s.marks3;
}
void display()
{

System.out.println(name+"\t"+regno+"\t"+marks1+"\t"+marks2+"\t"+mar
ks3);
}
}
package student;
public class studentdemo {

public static void main(String[] args) {

student s1=new student();


student s2=new student("john",34266,58,96,84);
student s3=new student(s1);
s1.display();
s2.display();
s3.display();

3. b for Expression Evalution:

Public class exp


{
public static void main(String[] args)
{
int a=10,b=5,c=8,d=2;
float x=6.4F, y=3.0F;
int answer2 = a*(b+c)/d;
float answer5=a/y;
int answer6=a%c;
boolean bool3=a<b||a>d;
System.out.println("a*(b+c)/d="+answer2);
System.out.println("a/y="+answer5);
System.out.println("a%c="+answer6);
System.out.println("a<b||a>d="+bool3);
}

}
c.to perform Autoboxing and Unboxing:

package hello;

public class Hlo {

public static void main(String[] args) {


int a =50;
Integer a3=Integer.valueOf(a);
Integer a4=a;
System.out.println("a3="+a3);
System.out.println("a4="+a4);
}

Unboxing:

package unboxing;
public class hello {

public static void main(String[] args) {


Integer obj =5;
int a = obj;
int b = obj;
System.out.println(a);
System.out.println(b);

}
5. Code execute and debug programs that uses different control statements:

A Write a java program to find whether given year is leapyear or not:

Public class leapyear {


public static void main(String[] args) {
int year=2000;
if (((year%4==0)&&(year%100==0)) ||(year%400==0)){
System.out.println("LEAP YEAR");
}
else {
System.out.println("COMMON YEAR");
}

b. Write a java program to check the given number is positive,negative or


zero:

public class negative {

public static void main(String[] args) {


int number =2;
if(number>0) {
System.out.println("POSITIVE");
} else if (number<0) {
System.out.println("NEGATIVE");
} else {
System.out.println("ZERO");
}

}
c. Write a java program to check for vowel or consonant:

package vowel;

public class vo {

public static void main(String[] args)


{
char ch='o';
switch(ch)
{
case 'a':
System.out.println("vowel");
break;
case 'e':
System.out.println("vowel");
break;
case 'i':
System.out.println("vowel");
break;
case 'o':
System.out.println("vowel");
break;
case 'u':
System.out.println("vowel");
break;
case'A':
System.out.println("vowel");
break;
case'E':
System.out.println("vowel");
break;
case'I':
System.out.println("vowel");
break;
case'O':
System.out.println("vowel");
break;
case'U':
System.out.println("vowel");
break;
default:
System.out.println("consonent");
}

D Write a java program to find sum of first 20 natural number using for
loop:

public class for loop {


public static void main(String[] args) {
int sum=0;
for (int x=1;x<20;x+1) {
sum=sum+x;
}
System.out.println("sum="+sum);

E Write a java program to check whether number is palindrome or not:

public class for loop {

public static void main(String[] args) {


int sum=0;
for (int x=1;x<20;x+1) {
sum=sum+x;
}
System.out.println("sum="+sum);

}
F Write the program to display 1 to 10 natural numbers (do while):

public class dowhile {

public static void main(String[] args) {


int i=1;
do {
System.out.println(i);
i++;
} while (i<=10);

}
6 CODE , EXECUTE AND DEBUG PROGRAMS
A)THAT USES ENCAPSULATION CONCEPT

public class EncapTest {


private String name;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int newAge) {
age = newAge;
}
public void setName(String newName) {
name = newName;
}
}

public class RunEncap {


public static void main(String args[]) {
EncapTest encap = new EncapTest();
encap.setName("MADHU");
encap.setAge(35);

System.out.print("Name:"+encap.getName()+"\nAge:"+encap.getAge());
}
}
b) define class and implement like simple calculator or text processing
complie with SRP.
public class calculator {
addition a = new addition();
subtraction s = new subtraction();
multiplication m = new multiplication();
division d = new division();

void performAddition(int x, int y) {


a.add(x,y);
}
void performSubtraction(int x, int y) {
s.sub(x, y);
}
void performMultiplication(int x, int y){
m.mul(x, y);
}
void performDivision(int x, int y) {
d.div(x, y);

}
}
public class addition {
int res;
void add(int a, int b) {
res = a + b;
System.out.println("Addition of two numbers ="+res);
}

}
public class subtraction {
int res;
void sub(int a, int b) {
res = a - b;
System.out.println("Subtraction of two numbers ="+ res);

}
public class multiplication {
int res;
void mul(int a, int b) {
res = a * b;
System.out.println("Multiplication of two numbers =" + res);
}

}
public class division {
int res;
void div(int a, int b) {
res = a / b;
System.out.println("Division of two number =" + res);
}

}
public class TestCalculator {
public static void main(String args[]) {
calculator c = new calculator();
c.performAddition(1, 2);
c.performSubtraction(10,2);
c.performMultiplication(10,5);
c.performDivision(10,2);
}
}
7 a) code , execute and debug programs toper form string manipulation .
public class stringdemo {

public static void main(String[] args) {


String s1=new String("gpt gulbarga");
String s2="GPT GULBARGA";
System.out.println("the string s1 is:"+s1);
System.out.println("the string s2 is:"+s2);
int a= s1.length();
System.out.println(" the lenght of the string s1 is :" +a);
System.out.println("the first accurence of r is at the
position:"+s1.indexOf('r'));
System.out.println("the String in Upper Case:" +s1.toUpperCase());
System.out.println("the string in Lower Case:" +s2.toLowerCase());
System.out.println("s1 equals to s2:"+s1.equals(s2));
System.out.println("s1 equals ignore case to
s2:"+s1.equalsIgnoreCase(s2));
int result=s1.compareTo(s2);
System.out.println("After compareTo()");
if(result==0)
System.out.println(s1+"is equal to"+s2);
else if(result>0)
System.out.println(s1+"is greater than to"+s2);
else
System.out.println(s1+"smaller than to"+s2);
System.out.println("Charecter at an index of 6 is:"+s1.charAt(6));
String s3=s1.substring(4,12);
System.out.println("Extracted substring is:"+s3);
System.out.println("After Replacing g with a in s1:"+s1.replace('g','a'));
String s4=" This is a book";
System.out.println("the string s4 is:"+s4);
System.out.println("After trim():"+s4.trim());
}
}
8a ) code , execute and debug programs that uses inheritance concepts
public class computerteacher extends teacher {
String mainSubject = "Computer";
public static void main(String args[]) {
computerteacher obj = new computerteacher();
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();

public class teacher {


String designation = "Teacher";
String collegeName = "Polytechnic";
void does() {
System.out.println("Teaching");
}
}
9. code execute and debug programs that uses
a. STATIC BINDING:

public class person


{
public void speak()
{
System.out.println("Person speak");
}
}
class Teacher extends person
{
public static void Speak()
{
System.out.println("Teacher speaks");
}
}

public class staticbinding {


public static void main(String args[])
{
person obj = new Teacher();
obj.speak();
person obj2 = new person();
obj2.speak();
}
}
b. DYNAMIC BINDING:
public class Teacher extends Person {
public void speak()
{
System.out.println("Teacher speaks");
}
}

public class Person {


public void speak()
{
System.out.println("Person speaks");
}
}

public class Dynamicbinding {


public static void main(String args[])
{
Person obj2 = new Person();
obj2.speak();
Person obj = new Teacher();
obj.speak();
}

You might also like