0% found this document useful (0 votes)
50 views59 pages

Mohd Aiman Saleem Java Record

The document contains 14 code snippets in Java that demonstrate various programming concepts like data types, operators, arrays, constructors, methods, recursion, etc. Each code snippet is followed by sample input/output. The snippets provide examples of printing data type properties, conversions between types, sorting arrays, default/parameterized constructors, method overloading, call by value/reference, finding prime numbers, solving quadratic equations, and generating the Fibonacci sequence.

Uploaded by

Hemanth Naik
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)
50 views59 pages

Mohd Aiman Saleem Java Record

The document contains 14 code snippets in Java that demonstrate various programming concepts like data types, operators, arrays, constructors, methods, recursion, etc. Each code snippet is followed by sample input/output. The snippets provide examples of printing data type properties, conversions between types, sorting arrays, default/parameterized constructors, method overloading, call by value/reference, finding prime numbers, solving quadratic equations, and generating the Fibonacci sequence.

Uploaded by

Hemanth Naik
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/ 59

DATE:28-11-2021

NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

/*1. Write a java program for printing of Date type’s size, minimum
and maximum values.*/

import java.io.*;

public class DataTypesDemo{


public static void main(String arr[]){
System.out.println("for an Integer");
System.out.println("SIZE is:"+Integer.SIZE);
System.out.println("min value is:"+Integer.MIN_VALUE);
System.out.println("max value is:"+Integer.MAX_VALUE);
System.out.println("for a Byte");
System.out.println("SIZE is:"+Byte.SIZE);
System.out.println("min value is:"+Byte.MIN_VALUE);
System.out.println("max value is:"+Byte.MAX_VALUE);
System.out.println("for a Float");
System.out.println("SIZE is:"+Float.SIZE);
System.out.println("min value is:"+Float.MIN_VALUE);
System.out.println("max value is:"+Float.MAX_VALUE);
System.out.println("for a Double type");
System.out.println("SIZE is:"+Double.SIZE);
System.out.println("min value is :"+Double.MIN_VALUE);
System.out.println("max value is:"+Double.MAX_VALUE);
}
}

OUTPUT:

1
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//2. Write a java program for Widening conversion.

import java.io.*;
public class Widening{
public static void main(String args[]){
int i=127;
double d=123.5;
byte b;
System.out.println("int to byte conversion");
b= (byte)i;
System.out.println(b);
System.out.println("double to int conversion");
i =(int)d;
System.out.println(i);
}
}

OUTPUT:

2
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//3. Write a java program for Narrowing conversion.


import java.io.*;
public class Narrowing{
public static void main(String args[]){
int i=127;
double d=123.5;
byte b;
System.out.println("int to byte conversion");
b= (byte)i;
System.out.println(b);
System.out.println("double to int conversion");
i =(int)d;
System.out.println(i);
}
}

OUTPUT:

3
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//4.Write a java program for sorting of numbers using Arrays.


import java.io.*;
public class SortNum{
public static void main(String args[]){
int number[] = {16,30,2,80,15};
int n = number.length;
System.out.println("length of an array is:"+n);
System.out.println("given list");
for(int i=0;i<n;i++){
System.out.println(" "+number[i]);
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(number[i]<number[j]){
int temp = number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
System.out.println("sorted list");
for(int i=0;i<n;i++){
System.out.println(" "+number[i]);
}
}
}

OUTPUT:

4
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//5.Write a java program for default constructor.


import java.util.*;

class Box{

double width;
double height;
double depth;

Box(){

width=10;
height=12;
depth=11;

}
public class Constructor{
public static void main(String args[]){
Box ob =new Box();
System.out.println("width :"+ob.width);
System.out.println("height :"+ob.height);
System.out.println("depth :"+ob.depth);

}
}

OUTPUT:

5
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//6.Write a java program for Parameterized constructor.


import java.util.*;

class Box{

double width;
double height;
double depth;

Box(){

width=10;
height=12;
depth=11;

Box(double a,double b,double c){


width=a;
height=b;
depth=c;
}

public class ParaConstructor{


public static void main(String args[]){
Box ob =new Box(2.5,1.2,2);
System.out.println("width :"+ob.width);
System.out.println("height :"+ob.height);
System.out.println("depth :"+ob.depth);

}
}

OUTPUT:

6
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//7.Write a java program for this keyword.


import java.util.*;

class Box{

double width;
double height;
double depth;

Box(){

width=10;
height=12;
depth=11;

Box(double width,double height,double depth){


this.width=width;
this.height=height;
this.depth=depth;
}

public class ParaConstructor{


public static void main(String args[]){
Box ob =new Box(2.5,1.2,2);
System.out.println("width :"+ob.width);
System.out.println("height :"+ob.height);
System.out.println("depth :"+ob.depth);

}
}

OUTPUT:

7
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//8.Write a java program for method overloading.


import java.io.*;
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
void test(int a) {
System.out.println("a: " + a);
}
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}

public class Overload{


public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}

OUTPUT:

8
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//9.Write a java program for constructor overloading.


class Box {
double width, height,depth;
Box(){
width=12.0;
height=5.0;
depth=4.8;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}
}
public class Overloading{
public static void main(String args[])
{
Box a=new Box(1,2,3);
double c=a.volume();
System.out.println(c);
}
}

OUTPUT:

9
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//10.Write a java program for call-by-value.


import java.io.*;
class Test{
void meth(int i,int j){
i*=2;
j/=2;
}
}
public class CallByvalue{
public static void main(String args[]){
Test ob=new Test();
int a=15,b=20;
System.out.println("a and b before call:"+a+" "+b);
ob.meth(a,b);
System.out.println("a and b after call:"+a+" "+b);
}
}

OUTPUT:

10
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//11.Write a java program for call-by-Reference.


import java.io.*;
class Test{
int a,b;
Test (int i,int j){
a=i;
b=j;
}
void meth(Test o){
o.a*=2;
o.b/=2;
}
}
public class CallByRef{
public static void main(String args[]){
Test ob=new Test(15,20);
System.out.println("ob.a and ob.b before call :"+ob.a+"
"+ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call :"+ob.a+" "+ob.b);
}
}

OUTPUT:

11
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//12.Write a program to print Prime numbers up to a given number.


import java.util.Scanner;
class prime
{
public static void main(String args[])
{
int n,i,count=0;
Scanner input=new Scanner(System.in);
System.out.println("Enter a number:");
n=input.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
System.out.println(n+" is prime");
}
else
{
System.out.println(n+" is not prime");
}
}
}

OUTPUT:

12
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//13.Write a program to print roots of a quadratic equation


ax2+bx+c=0.
import java.util.*;

public class Quad{

public static void main(String args[]){

Scanner s=new Scanner(System.in);

System.out.println("enter the values for a,b & c");

int a=s.nextInt();

int b=s.nextInt();

int c=s.nextInt();

double d,e;

d=((b*b)-(4*a*c));

if(d==0){

System.out.println("expression has real&equal roots");

e=(-b)/(2*a);

System.out.print("roots are:"+e+"\t"+e);

if(d>0){

System.out.println("expression has real&unreal roots");

e=(-b+Math.sqrt(d))/(2*a);

System.out.print("roots are:"+e+"\t");

e=(-b-Math.sqrt(d))/(2*a);

System.out.println(e);

if(d<0){

System.out.println("expression has imaginary roots");

13
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

OUTPUT:

14
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

//14.Write a program to print Fibonacci sequence up to a given


number.
import java.io.*;
import java.util.Scanner;
public class Fib{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int i,a=0,b=1,c=0,t;
System.out.println("enter value of t");
t=input.nextInt();
System.out.print(a);
System.out.print(" "+b);
for (i=0;i<t-2;i++){
c=a+b;
a=b;
b=c;
System.out.print(" "+c);
}
System.out.println();
System.out.println(t+"th value of the series is :"+c);
}
}

OUTPUT:

15
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

16
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

17
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

18
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

19
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

20
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

21
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

22
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

23
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

24
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

25
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

26
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

27
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

28
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

29
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

30
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

31
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

32
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

33
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

34
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

35
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

36
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

37
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

38
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

39
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

40
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

41
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

42
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

43
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

44
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

45
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

46
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

47
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

48
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

49
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

50
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

51
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

52
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

53
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

54
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

55
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

56
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

57
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

58
DATE:28-11-2021
NAME: RATHNAVATH HEMANTH NAIK ROLL NO:20311A12E7 SECTIION :IT(F3)

59

You might also like