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

Java

The document contains a comprehensive guide on various Java programming concepts, including printing output, data type conversions, arrays, methods, constructors, inheritance, and conditional statements. It provides code examples and outputs for each topic, demonstrating practical applications of Java programming. Additionally, it covers advanced topics like method overloading, overriding, and AWT programming.

Uploaded by

riya.s1332002
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)
2 views

Java

The document contains a comprehensive guide on various Java programming concepts, including printing output, data type conversions, arrays, methods, constructors, inheritance, and conditional statements. It provides code examples and outputs for each topic, demonstrating practical applications of Java programming. Additionally, it covers advanced topics like method overloading, overriding, and AWT programming.

Uploaded by

riya.s1332002
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/ 19

INDEX

1 Print hello without object

2 boolean data type with do while loop

3 Convert integer into double

4 Convert double into integer

5 Print array
 1D Array
 2D Array

6 Method
 method overloading
7 Constructor
 Default
 Argument
8 constructor overloading

9 Overriding

10 Inheritance
 Single inheritance
 Multilevel inheritance
 Multiple inheritance

11 Conditional statement
 If
12 Super class

13 For each loop

14 Fobnacci series

15 Wap to calculate factorial

16 Wap to Print Star Pattern

17 Awt program

ANKIT SINGH 230031150013 Page 1


1 . Print hello without object
public class hello {

public static void main(String[] args) {

System.out.println("hello");

Output
PS C:\Users\DELL> cd "e:\ankit\v s code files\java\college\" ; if ($?) { javac hello.java } ; if ($?)
{ java hello }

Hello

2. Boolean data type with do while loop


import java.util.*;

public class checkdivby7 {

public static void main(String arg[]) {

Scanner sc = new Scanner(System.in);

int num =sc.nextInt();

boolean divisivleby7=false;

do { System.out.println(num);

if(num%7==0)

divisivleby7=true;

num--;

} while (divisivleby7==false);

ANKIT SINGH 230031150013 Page 2


Output
9

3 . Convert integer into double


public class typecasting{

public static void main(String[] args) {

int n=10;

System.out.println(n);

//convert into int to double data type

double m=n;

System.out.println(m);

Output
10

10.0

4 . Convert double into integer

public class typecasting2 {

public static void main(String[] args) {

double num= 10.99;

System.out.println(num);

ANKIT SINGH 230031150013 Page 3


//convert double into int

int data=(int) num;

System.out.println(data);

Output
10.99

10

5 . Print 1D array and 2D array

 1D Array
public class array1 {

public static void main(String[] args) {

int arr[]=new int[6];

arr[0]=1;

arr[1]=2;

arr[3]=5;

arr[4]=-3;

arr[5]=6;

arr[2]=6;

for(int i=0;i<arr.length;i++){

System.out.println(arr[i]);

ANKIT SINGH 230031150013 Page 4


}

Output
1

-3

 2D array
import java.util.Scanner;

public class twodarray {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("input row");

int n=sc.nextInt();

System.out.println("input column");

int m=sc.nextInt();

int arr[][]=new int[n][m];

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

arr[i][j]=sc.nextInt();

System.out.println();

ANKIT SINGH 230031150013 Page 5


for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

System.out.println(arr[i][j]);

System.out.println();

Output
input row

input column

21

34

12

23

65

74

46

36

38

213412

ANKIT SINGH 230031150013 Page 6


236574

463638

6. Method and method overloading

 Method

public class method {

public static void main(String[] args) {

int a=10;

int b=13;

float c=20.99f;

float d=20.99f;

ankit(a,b);

ankit(c,d);

public static void ankit(int i,int j){

int s=i+j;

System.out.println("sum is="+s);

public static void ankit(float i,float j){

float s=i+j;

System.out.println("sum is="+s);

ANKIT SINGH 230031150013 Page 7


}

Output
sum is=23

sum is=41.98

 method overloading
public class overloading {

public static void main(String[] args) {

int a=10;

int b=20;

float c=10.88f;

float d=12.4f;

int sum=ankit(a,b);

float sums=ankit(c,d);

System.out.println("sum="+sum);

System.out.println("sum="+sums);

public static int ankit(int i,int j){

int p=i+j;

return p;

public static float ankit(float i,float j){

float p=i+j;

return p;

ANKIT SINGH 230031150013 Page 8


}

Output
sum=30

sum=23.279999

7. Constructor and default,argument

public class constr {

constr(){

System.out.println("connect");

public static void main(String[] args) {

constr as=new constr();

constr av=new constr();

Output
connect

connect

8 .Cunstructor overloading
public class constover {

public static void main(String[] args) {

constover sc=new constover();

int a=6;

int b=6;

ANKIT SINGH 230031150013 Page 9


constover ss=new constover(a,b);

constover(){

System.out.println("overloading");

constover(int a,int b){

int s=a+b;

System.out.println(s);

Output

overloading

12

9 .OVERRIDING

class A {

void sayHello() { System.out.print("HELLO A :)"); }

class B extends A{

void sayHello(){ System.out.print("HELLO B :)"); }

class override{

public static void main (String [] args){

A obj = new A();

ANKIT SINGH 230031150013 Page 10


obj.sayHello();

OUTPUT
HELLO B :

10. Inheritence

 Single inheritance

class singleheritence {

void child(){

System.out.println("parent class");

class parent extends singleheritence{

public static void parents(){

System.out.println("child class");

public static void main(String[] args) {

parent acc=new parent();

acc.child();

acc.parents();

ANKIT SINGH 230031150013 Page 11


}

output
parent class

child class

 multiple inheritance

interface a{
public void run();
}
interface b{
public void run();
}
class c implements a,b {
public void run(){
System.out.println("c class method");

}
}

public class multipleinheritence {


public static void main(String[] args) {
c obj =new c();
obj.run();
}

}
output
c class method

11. Conditional statement

 if
import java.util.*;

public class conditional{

public static void main(String args[]){

Scanner sc= new Scanner(System.in);

int n = sc.nextInt();

ANKIT SINGH 230031150013 Page 12


if(n%2==0){System.out.println("number is even");}

else{System.out.println("number is odd");}

Out put
7

number is odd

12. super class


class parent {

int a=8;

void about(int a){

System.out.println("lara");

System.out.println("singh");

class child extends parent{

void about(){

super.about(43);

System.err.println("ankit");

System.err.println("singh");

public class superc {

public static void main(String[] args) {

ANKIT SINGH 230031150013 Page 13


child obj = new child();

obj.about();

Output
lara

singh

ankit

singh

13. For each loop


public class foreach {

public static void main(String[] args) {

int arr[]={1,2,3,4,5,6,7};

for(int a:arr){

System.out.println(a);

Output
1

ANKIT SINGH 230031150013 Page 14


6

14. FABONACCI SERIES


import java.util.Scanner;

public class fabonacciSeries {

static void fab( int a , int b,int c) {

if(a==0&b==1) System.out.print(a+" , "+b);

if (c>0){

c--;

int x=a+b;

System.out.print(" , "+x);

fab (b,x,c);

public static void main(String[] args) {

Scanner sc = new Scanner (System.in);

System.out.println("Enter any no : ");

int n = sc.nextInt();

fab(0,1,n);

Output
0,1,1,2,3,5,8

ANKIT SINGH 230031150013 Page 15


15 .Wap to Print Star Pattern

public class star {

public static void main (String [] args){

for(int i=0 ;i<5 ;i++) {

for(int j=i ;j<5 ;j++) {

System.out.print("*");

System.out.print("\n");

OUTPUT

*****
****
***
**
*

16. Wap to calculate factorial


import java.util.Scanner;

public class factorial {

public static void main(String[] args) {

Scanner sc = new Scanner (System.in);

ANKIT SINGH 230031150013 Page 16


int f=1;

System.out.println("input any number : ");

int n =sc.nextInt();

for (int i=1;i<=n;i++){

f = i*f;

System.out.println("factorial : "+f);

Output
input any number :

120

17 . Awt program
import java.awt.*;

import java.awt.event.*;

public class awtcheckbok {

public static void main(String[] args) {

Frame f = new Frame("BUTTON EXample");

final TextField tf = new TextField();

tf.setBounds(200, 100, 120, 200);

tf.setBackground(Color.RED);

Button b=new Button("Click Here");

b.setBounds(30,50,60,30);

b.setBackground(Color.CYAN);

ANKIT SINGH 230031150013 Page 17


Checkbox ck =new Checkbox("java");

ck.setBounds(40,150,100,20);

Checkbox cks =new Checkbox("python");

cks.setBounds(40,190,100,20);

f.addWindowListener(new WindowAdapter() {

public void WindowClosing(WindowEvent e){

f.dispose();

});

b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

tf.setText("Welcome Riya");

});

f.add(b);

f.add(tf);

f.add(cks);

f.add(ck);

f.setSize(500, 400);

f.setLayout(null);

f.setVisible(true);

Output

ANKIT SINGH 230031150013 Page 18


ANKIT SINGH 230031150013 Page 19

You might also like