0% found this document useful (0 votes)
35 views7 pages

JP Lab File

java programming

Uploaded by

Aditya Chugh
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)
35 views7 pages

JP Lab File

java programming

Uploaded by

Aditya Chugh
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/ 7

AMITY UNIVERSITY HARYANA

SESSION : 2022-2026
SUBMITTED BY: MUKUL KUMAR VERMA
SUBMITTED TO: DR. SURJEET DALAL
COURSE: BTECH(CSE)
SEM:5
ENROLLMENT NO : A50105222066
SUBJECT: JAVA PROGRAMMING LAB

• PROGRAM TO PRINT HELLo in java

public class P1 {
public static void main(String[] args) {
System.out.println("Hello , it's first program in java");
}
}

Output:-
• SUM OF TWO NUMBERS AS USER INPUT.

import java.util.Scanner;
public class P2 {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter first number:");
int a = sc.nextInt();
System.out.println("Enter second number:");
int b = sc.nextInt();
int sum = a + b;
System.out.println("Addition of two numbers is:"+ sum);
}
}

Output:-

• SUM OF TWO FLOAT NUMBERS IN JAVA

import java.util.Scanner;
public class P3{
public static void main(String[] args){
try{
System.out.println("Enter the two float values : ");
Scanner sc = new Scanner(System.in);
float a = sc.nextFloat();
float b = sc.nextFloat();
System.out.println("sum is = " + Float.sum(a, b));
}
catch(Exception e)
{
System.out.println("Invalid Input");
}
}
}
Output:-

• CHANGED CASE OF ENTERED CHARACTER

import java.util.Scanner;
public class P4 {
public static void main(String[] args) {
System.out.println("Enter the String : ");
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
StringBuffer newStr=new StringBuffer(a);
for(int i = 0; i < a.length(); i++) {
if(Character.isLowerCase(a.charAt(i))) {
newStr.setCharAt(i, Character.toUpperCase(a.charAt(i)));
}
else if(Character.isUpperCase(a.charAt(i))) {
newStr.setCharAt(i, Character.toLowerCase(a.charAt(i)));
}
}
System.out.println("String after case conversion : " + newStr);
}
}
Output:-

• MAXIMUM OF THREE INTEGERS AS USER


INPUT

import java.util.Scanner;
public class P5{
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter first number:");
int a = sc.nextInt();
System.out.println("Enter second number:");
int b = sc.nextInt();
System.out.println("Enter third number:");
int c = sc.nextInt();
if (a > b && a >c){
System.out.println("Greatest number is:" +a);
}
else if(b>a && b>c){
System.out.println("Greatest number is:" +b);
}
Output:-

• Program to find out the greatest out of ten


numbers stored using arrays

public class lab6 {


public static void main(String[] args) {
int[] numbers = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int max = -999999;
for(int i = 0; i < 10; i++)
{
if(numbers[i] > max)
{
max = numbers[i];
}
else
{
continue;
}
}

System.out.println("The largest number in the array is: " + max);


}
}

Output:-

• Program to create class with Name as String and and Age as integer data
members. The class should have two methods to take input from the user
and display the data.

import java.util.Scanner;

public class lab7 {

static class nameAndAge {


String name;
int age;

void input(){
Scanner sc = new Scanner(System.in);
System.out.print("Enter your Name: ");
name = sc.nextLine();
System.out.print("Enter your Age: ");
age = sc.nextInt();
sc.close();
}

void display(){
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public static void main(String[] args) {
nameAndAge ob = new nameAndAge();
ob.input();
ob.display();
}
}

Output:-

• Program to find the factorial of a number using class and object

import java.util.Scanner;

public class lab8 {

static class Fact {


int num;
int factnum;
public Fact(int num) {
this.num = num;
factnum = 1;
for (int i = num; i > 0; i--) {
factnum = factnum * i;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
Fact fact = new Fact(num);
System.out.println("Factorial of number is " + fact.factnum);

sc.close();
}
}

Output:-

• Program on method of overloading :-


public class MethodOverloadingExample {

public int add(int a, int b) {


return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}

public double add(double a, double b) {


return a + b;
}

public double add(int a, double b) {


return a + b;
}

public static void main(String[] args) {


MethodOverloadingExample obj = new MethodOverloadingExample();

System.out.println("Sum of 5 and 10: " + obj.add(5, 10));

System.out.println("Sum of 5, 10, and 15: " + obj.add(5, 10, 15));

System.out.println("Sum of 5.5 and 10.5: " + obj.add(5.5, 10.5));

System.out.println("Sum of 5 and 10.5: " + obj.add(5, 10.5));


}
}

OUTPUT

• Program on method of overiding :-


// Superclass
class Shape {

public void area() {


System.out.println("Area of the shape.");
}
}

// Subclass: Square
class Square extends Shape {
private double side; // Side length of the square

public Square(double side) {


this.side = side;
}

@Override
public void area() {
double area = side * side;
System.out.println("Area of the square: " + area);
}
}

// Subclass: Rectangle
class Rectangle extends Shape {
private double length, width; // Length and width of the rectangle

public Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

@Override
public void area() {
double area = length * width;
System.out.println("Area of the rectangle: " + area);
}
}

public class MethodOverridingExample {


public static void main(String[] args) {

Shape square = new Square(5); // A square with side 5


Shape rectangle = new Rectangle(4, 6); // A rectangle with length 4 and width
6

square.area(); // Calls the overridden method in Square class


rectangle.area(); // Calls the overridden method in Rectangle class
}
}
OUTPUT

You might also like