Java Programming Part A

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

JAVA PROGRAMMING

: INDEX – PART A :

Sl. Name of Program Date of Sign of


No submission Faculty
Java program to assign two integer values to X and Y.
01 Using the ‘if’ statement the output of the program
should display a message whether X is greater than Y.
02 Java program to list the factorial of the numbers 1 to
10. To calculate the factorial value, use while loop.
03 Java program to find the area and circumference of
the circle.
04 Program to use function overloading method.
Program to perform mathematical operations. Create a
05 class called AddSub with methods to add and subtract.
Create another class called MulDiv that extends from
AddSub class to use the member data of the super class.
Program with class variable that is available for all
06 instances of a class. Use static variable declaration.
Observe the changes that occur in the object’s member
variable values.
07 Program to create a student class with following
attributes; Enrollment No: Name, Mark of sub1, Mark
of sub2, mark of sub3, Total Marks.
08 Write a program to demonstrate multiple inheritance
and use of Implementing Interfaces
09 Write a program to demonstrate multilevel inheritance
using abstract class.

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 1


JAVA PROGRAMMING

1. Java program to assign two integer values to X and Y. Using the ‘if’ statement the
output of the program should display a message whether X is greater than Y.

import java.util.*;
public class Comparision
{
public static void main(String[] args)
{
int x,y;
System.out.println("Enter two operands");
Scanner sc = new Scanner(System.in);
x= sc.nextInt();
y= sc.nextInt();
if(x>y)
{
System.out.println(x + " is greater");
}
else
{
System.out.println(y + " is greater");
}
}
}

OUTPUT:

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 2


JAVA PROGRAMMING

2. Java program to list the factorial of the numbers 1 to 10. To calculate the factorial
value, use while loop. (Hint: Fact of 4 = 4*3*2*1)

import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{
int fact = 1;
int i = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number whose factorial is to be found: ");
int num = sc.nextInt();
while( i <= num )
{
fact = fact * i;
i++;
}
System.out.println("\n Factorial of " + num + " is: " + fact);
}
}

OUTPUT:

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 3


JAVA PROGRAMMING

3. Java program to find the area and circumference of the circle.

import java.util.Scanner;
public class Circle123
{
public static void main(String[] args)
{
double radius, area, circum;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Radius of Circle: ");
radius = s.nextFloat();
area = Math.PI*radius*radius;
System.out.println("\nArea = " +area);
circum= Math.PI * 2*radius;
System.out.printf( "\n Circumference is: %.2f",circum) ;
}
}

OUTPUT:

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 4


JAVA PROGRAMMING

4. Program to use function overloading method.

public class MethodOverloading


{
public static void main(int args)
{
System.out.println("Main Method with int argument Executing");
System.out.println(args);
}
public static void main(char args)
{
System.out.println("Main Method with char argument Executing");
System.out.println(args);
}
public static void main(Double[] args)
{
System.out.println("Main Method with Double Executing");
System.out.println(args);
}
public static void main(String[] args)
{
System.out.println("Original main Executing");
MethodOverloading.main(12);
MethodOverloading.main('c');
MethodOverloading.main(1236);
}
}

OUTPUT:

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 5


JAVA PROGRAMMING

5. Program to perform mathematical operations. Create a class called AddSub with


methods to add and subtract. Create another class called MulDiv that extends
from AddSub class to use the member data of the super class. MulDiv should have
methods to multiply and divide A main function should access the methods and
perform the mathematical operations.

class addsub
{
int num1;
int num2;
addsub(int n1, int n2)
{
num1 = n1;
num2 = n2;
}
int add()
{
return num1+num2;
}
int sub()
{
return num1-num2;
}

class multdiv extends addsub


{
public multdiv(int n1, int n2)
{
super(n1, n2);
}
int mul()
{
return num1*num2;
}
float div()
{
return num2/num1;
}
float modulus()

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 6


JAVA PROGRAMMING

{
return num2%num1;
}

public class adsb


{
public static void main(String arg[])
{
addsub r1=new addsub(50,20);
int ad = r1.add();
int sb = r1.sub();
System.out.println("Addition =" +ad);
System.out.println("Subtraction =" +sb);
multdiv r2 =new multdiv(18,20);
int ml = r2.mul();
float dv =r2.div();
float mod = r2.modulus();
System.out.println("Multiply =" +ml);
System.out.println("Division =" +dv);
System.out.println("Modulus =" +mod);
}
}

OUTPUT:

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 7


JAVA PROGRAMMING

6. Program with class variable that is available for all instances of a class. Use static
variable declaration. Observe the changes that occur in the object’s member
variable values.

class Student
{
int rollno;//instance variable
String name;
static String college = " GFGC HOSADURGA ";//static variable
//constructor
Student(int r, String n)
{
rollno = r;
name = n;
}
//method to display the values
void display ()
{
System.out.println( "\t Roll No student is = " +rollno+ "\n \t Name of
student is = " +name+ " \n \t Name of college is =" +college);
}
}
//Test class to show the values of objects
public class Static_variable
{
public static void main(String args[])
{
Student s1 = new Student(111101,"NANDAN");
Student s2 = new Student(111102,"ARYAN");
//we can change the college of all objects by the single line of code \n
//Student.college="GFGC";
s1.display();
s2.display();
}
}

OUTPUT:

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 8


JAVA PROGRAMMING

7. Program to create a student class with following attributes; Enrollment No:


Name, Mark of sub1, Mark of sub2, mark of sub3, Total Marks. Total of the three
marks must be calculated only when the student passes in all three subjects. The
passingmark for each subject is 50. If a candidate fails in any one of the subjects
his total mark must be declared as zero. Using this condition write a constructor
for this class. Write separate functions for accepting and displaying student
details. In the main method create an array of three student objects and display
the details.

import java.util.Scanner;
public class Student_details
{
public static void main(String[] args)
{
Student s1 = new Student("1001","CHETAN",91,89,79);
s1.printStudentDetails();
Student s2 = new Student();
s2.getStudentDetails();
s2.printStudentDetails();
}
}

class Student
{
String usn;
String name;
int marks1,marks2,marks3;
int totalMarks;
Student()
{

}
Student(String usn,String name,int marks1,int marks2,int marks3)
{
this.usn = usn;
this.name = name;
this.marks1 = marks1;
this.marks2 = marks2;
this.marks3 = marks3;
if(marks1 >= 50 & marks2 >= 50 & marks3 >= 50)
totalMarks = marks1 + marks2 + marks3;
else totalMarks =0;
}

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 9


JAVA PROGRAMMING

void getStudentDetails()
{
System.out.println("Enter usn,name,marks of three subjects");
Scanner sc = new Scanner(System.in);
usn = sc.next();
name = sc.next();
marks1 = sc.nextInt();
marks2 = sc.nextInt();
marks3 = sc.nextInt();
if(marks1 >= 50 & marks2 >= 50 & marks3 >= 50)
totalMarks = marks1 + marks2 + marks3;
else totalMarks =0;
}
void printStudentDetails()
{
System.out.println("USN: " +this.usn);
System.out.println("Name: " +this.name);
System.out.println("Sub1 marks: " +this.marks1);
System.out.println("Sub2 marks: " +this.marks2);
System.out.println("Sub3 marks: " +this.marks3);
System.out.println("Total marks: " +this.totalMarks);
}
}

OUTPUT:

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 10


JAVA PROGRAMMING

8. Write a program to demonstrate multiple inheritance and use of Implementing


Interfaces

// CNG Car interface


interface CNG_Car
{
// Abstract methods
void drive();
void cng_kit();
}

// Petrol Car interface


interface Petrol_Car
{
// Abstract methods
void drive();
void petrol_kit();
}

// Multiple Inheritance using Interface


class Hybrid_Car implements Petrol_Car, CNG_Car
{
public void drive()
{
System.out.println("Driving a Hybrid Car");
}
// Overridden method of CNG_Car Interface
public void cng_kit()
{
System.out.println("Using the CNG kit for Hybrid Car");
}
// Overridden method of Petrol_Car Interface
public void petrol_kit()
{
System.out.println("Using the Petrol kit for Hybrid Car");
}
}

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 11


JAVA PROGRAMMING

// Driver Code
class Car_attributes
{
public static void main(String args[])
{
// Creating a new object of the Hybrid Car class
Hybrid_Car obj = new Hybrid_Car();
// Calling the methods of the Hybrid_Car class
obj.drive();
obj.cng_kit();
obj.petrol_kit();
}
}
OUTPUT:

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 12


JAVA PROGRAMMING

9. Write a program to demonstrate multilevel inheritance using abstract class.

public class Multilevel_inheritence


{
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}

abstract class Car


{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}

class Maruti extends Car


{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
System.out.println("Max: 90Kmph");
}
}

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 13


JAVA PROGRAMMING

class Maruti800 extends Maruti


{
public Maruti800()
{
System.out.println("Maruti Model: 800");
}
@Override
public void speed()
{
System.out.println("Max: 80Kmph");
}
}

OUTPUT:

CHETAN M, Lecturer in Dept. Of BCA, GFGC HOSADURGA Page 14

You might also like