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

java orginal

The document contains a series of Java programming assignments completed by Hashith Gowda N, showcasing various programming concepts such as input/output, conditionals, loops, arrays, and object-oriented programming. Each question includes code snippets that demonstrate the implementation of specific tasks, such as calculating totals, checking conditions, and using classes and methods. The assignments illustrate a progression from basic programming tasks to more complex concepts like method overloading and overriding.

Uploaded by

hashithgowda357
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

java orginal

The document contains a series of Java programming assignments completed by Hashith Gowda N, showcasing various programming concepts such as input/output, conditionals, loops, arrays, and object-oriented programming. Each question includes code snippets that demonstrate the implementation of specific tasks, such as calculating totals, checking conditions, and using classes and methods. The assignments illustrate a progression from basic programming tasks to more complex concepts like method overloading and overriding.

Uploaded by

hashithgowda357
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

JAVA ASSIGNMENT

Question 1:My First Program.

// USN : 1MJ23IS026 Name: Hashith Gowda N

import java.util.Scanner;

public class Main {

public static void main (String[]args){

// USN : 1MJ23IS026 Name: Hashith Gowda N

System.out.println("Hello World");

// USN : 1MJ23IS026 Name: Hashith Gowda N

OUTPUT:
JAVA ASSIGNMENT

Question 2: Input age and name in program itself.

// USN : 1MJ23IS026 Name: Hashith Gowda N

import java.util.Scanner;

public class Main{

// USN : 1MJ23IS026 Name: Hashith Gowda N

public static void main( String[] args){

int age = 19;

String name = "Hashith Gowda N";

System.out.println("Name : "+ name);

System.out.println("Age : " + age);

// USN : 1MJ23IS026 Name: Hashith Gowda N

OUTPUT:
JAVA ASSIGNMENT

Queston 3:Input age and name from user through scanner class.

// USN : 1MJ23IS026 Name: Hashith Gowda N

import java.util.Scanner;

public class Main {

public static void main(String[] args){

String name;

int age;

// USN : 1MJ23IS026 name: Hashith Gowda N

Scanner sc = new Scanner(System.in);

System.out.print("Enter your name:");

name=sc.nextLine();

System.out.print("Enter your age:");

age=sc.nextInt();

System.out.println("Name = "+ name + "\n Age = "+age);

// USN : 1MJ23IS026 Name: Hashith Gowda N

OUTPUT:
JAVA ASSIGNMENT

Question 4:Total and avg of 3 marks.

// USN : 1MJ23IS026 Name: Hashith Gowda N

import java.util.Scanner;

public class Main {

public static void main(String[] args){

String Name;

int Rollno;

int M1,M2,M3,Total;

float avg;

// USN : 1MJ23IS026 Name: Hashith Gowda N

Scanner sc = new Scanner(System.in);

System.out.print("Enter your name:");

Name = sc.nextLine();

System.out.print("Enter your roll no:");

Rollno=sc.nextInt();

System.out.print("Enter M1,M2,M3:");

M1=sc.nextInt();

M2=sc.nextInt();

M3=sc.nextInt();

// USN : 1MJ23IS026 Name: Hashith Gowda N

Total = M1+M2+M3;

avg = Total/3;
JAVA ASSIGNMENT

System.out.println("Name:"+Name+"\nRollno:"+Rollno+"\nTotal Marks:"+Total+"\nAvg:"+avg);

// USN : 1MJ23IS026 Name:Hashith Gowda N.

OUTPUT:

.
JAVA ASSIGNMENT

Question 5:Check whether given number is odd or even.

// USN : 1MJ23IS026 Name: Hashith Gowda N

import java.util.Scanner;

public class Main {

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

int num;

System.out.print("Enter the number:");

num = sc.nextInt();

if( num % 2 == 0 )

System.out.println("The entered number is even");

else

System.out.println("Entered number is odd");

// USN : 1MJ23IS026 Name: Hashith Gowda N

OUTPUT:
JAVA ASSIGNMENT

Question 6:Check whether pass or fail.

// USN : 1MJ23IS026 Name: Hashith Gowda N

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// USN : 1MJ23IS026 Name:Hashith Gowda N

Scanner sc = new Scanner(System.in);

int m1,m2,m3;

System.out.print("Enter the m1,m2,m3:");

m1=sc.nextInt();

m2=sc.nextInt();

m3=sc.nextInt();

if ( m1 >= 50 && m2 >= 50 && m3 >= 50) {

System.out.println("The student is pass !!");

else {

System.out.println("The student is failed !!");

}// USN : 1MJ23IS026 Name:Hashith Gowda N

}
JAVA ASSIGNMENT

OUTPUT:
JAVA ASSIGNMENT

Question 7:Max out of two numbers.

// USN : 1MJ23IS026 Name:Hashith Gowda N

import java.util.Scanner;

public class Main {

public static void main(String[]args){

int a,b;

Scanner sc=new Scanner(System.in);

System.out.print("Enter a and b:");

// USN : 1MJ23IS026 Name: Hashith Gowda N

a = sc.nextInt();

b = sc.nextInt();

if( a > b ) {

System.out.println("Maximum is a , i.e : "+a);

} else {

System.out.println("Maximum is b , i.e : "+b);

// USN : 1MJ23IS026 Name: Hashith Gowda N

OUTPUT:
JAVA ASSIGNMENT

Question 8:Write a program to find matured Amount.

// USN : 1MJ23IS026 Name: Hashith Gowda N

import java.util.Scanner;

public class Main{

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the principal amount: ");

double principal = in.nextDouble();

System.out.print("Enter your age: ");

int age = in.nextInt();

double interestRate;

if (age < 18) {

interestRate = 2.0;

} else if (age <= 60) {

interestRate = 5.0;

} else {

interestRate = 8.0;

double maturedAmount = principal + (principal * interestRate /100);

System.out.printf("The matured amount is: %.2f\n", maturedAmount);

// USN : 1MJ23IS026 Name: Hashith Gowda N

OUTPUT:
JAVA ASSIGNMENT

Question 9: Write a program for following condition using switch gold (g), silver (s),
platinum (p), Premium (r), other(o).

// USN : 1MJ23IS026 Name: Hashith Gowda N

import java.util.Scanner;

class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Hashith Gowda N,1MJ23IS026 ");

System.out.println("Enter your membership type:");

System.out.println("Gold (g), Silver (s), Platinum (p), Premium (r), Others (o)");

char membershipType = scanner.next().toLowerCase().charAt(0);

switch (membershipType) {

case 'g':

System.out.println(" Gold Membership You get a 20% discount.");

break;

case 's':

System.out.println("Silver Membership You get a 15% discount.");

break;

case 'p':

System.out.println(" Platinum Membership You get a 40% discount and free shipping.");

break;

case 'r':

System.out.println("Premium Membership You get a 50% discount and a free gift.");

break;

case 'o':

System.out.println("Guest! You can enjoy a 5% discount.");

break;

default:

System.out.println("Invalid membership type. Please enter g, s, p, r, or o.");

break;
JAVA ASSIGNMENT

// USN : 1MJ23IS026 Name: Hashith Gowda N

scanner.close();

OUTPUT:
.
JAVA ASSIGNMENT

Question 10:Switch case Days of Week.

import java.util.Scanner;

public class Main {

// USN : 1MJ23IS026 Name: Hashith Gowda N

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number (1-7): ");

int num = scanner.nextInt();

switch (num) {

case 1:

System.out.println("Monday");

break;

case 2:

System.out.println("Tuesday");

break;

case 3:

System.out.println("Wednesday");

break;

case 4:

System.out.println("Thursday");

break;

case 5:

System.out.println("Friday");

break;

case 6:

System.out.println("Saturday");

break;

case 7:

System.out.println("Sunday");
JAVA ASSIGNMENT

break;

default:

System.out.println("Invalid input! .");

break;

// USN : 1MJ23IS026 Name: Hashith Gowda N

OUTPUT:
JAVA ASSIGNMENT

Question 11:Max of Array

import java.util.Scanner;

public class Main {

// USN : 1MJ23IS026 Name:Hashith Gowda N

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");

int size = scanner.nextInt();

int[] numbers = new int[size];

System.out.println("Enter " + size + " elements:");

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

numbers[i] = scanner.nextInt();

int max = numbers[0];

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

if (numbers[i] > max) {

max = numbers[i];

// USN : 1MJ23IS026 Name: Hashith Gowda N

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

// USN : 1MJ23IS026 Name: Hashith Gowda N


JAVA ASSIGNMENT

OUTPUT:
JAVA ASSIGNMENT

Question 12:Sum of Array.

import java.util.Scanner;

// USN : 1MJ23IS026 Name: Hashith Gowda N

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");

int size = scanner.nextInt();

int[] numbers = new int[size];

System.out.println("Enter " + size + " elements:");

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

numbers[i] = scanner.nextInt();

int sum = 0;

for (int number : numbers) {

sum += number;

System.out.println("The sum of the array is: " + sum);

}// USN : 1MJ23IS026 Name: Hashith Gowda N

OUTPUT:
JAVA ASSIGNMENT

Question 13:Minimum of Array.

// USN : 1MJ23IS026 Name: Hashith Gowda N

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");

int size = scanner.nextInt();

int[] numbers = new int[size];

System.out.println("Enter " + size + " elements:");

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

numbers[i] = scanner.nextInt();

// USN : 1MJ23IS026 Name:Hashith Gowda N

int min = numbers[0];

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

if (numbers[i] < min) {

min = numbers[i];

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

scanner.close();

}// USN : 1MJ23IS026 Name: Hashith Gowda N


JAVA ASSIGNMENT

OUTPUT:
JAVA ASSIGNMENT

Question 14:Write 2D array and Give sum of Each row.

import java.util.Scanner;

// USN : 1MJ23IS026 Name: Hashith Gowda N

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int[][] array = new int[3][3];

System.out.println("Enter 9 elements for a 3x3 array:");

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

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

System.out.print("Element [" + i + "][" + j + "]: ");

array[i][j] = scanner.nextInt();

// USN : 1MJ23IS026 Name: Hashith Gowda N

System.out.println("\nSum of each row:");

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

int sum = 0;

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

sum += array[i][j];

System.out.println("Sum of row " + (i + 1) + ": " + sum);

// USN : 1MJ23IS026 Name: Hashith Gowda N


JAVA ASSIGNMENT

OUTPUT:
JAVA ASSIGNMENT

Question 15 : Write a program to create a Student class and display the details like name,
class and roll no.

// USN : 1MJ23IS026 Name: Hashith Gowda N

import java.util.Scanner;

class Student {

private String name;

private String studentClass;

private int rollNo;

// USN : 1MJ23IS026 Name: Hashith Gowda N

public Student(String name, String studentClass, int rollNo) {

this.name = name;

this.studentClass = studentClass;

this.rollNo = rollNo;

// USN : 1MJ23IS026 Name: Hashith Gowda N

public void displayDetails() {

System.out.println("Student Details:");

System.out.println("Name: " + name);

System.out.println("Class: " + studentClass);

System.out.println("Roll No: " + rollNo);

N}// USN : 1MJ23IS026 Name: Hashith Gowda N

public class Main {

public static void main(String[] args) {

// Create a Student object

Student student = new Student("John Doe", "10th Grade", 25);

// Display student details

student.displayDetails();

}
JAVA ASSIGNMENT

}}// USN : 1MJ23IS026 Name: Hashith Gowda N

OUTPUT:
JAVA ASSIGNMENT

Question 16 : Write a java program to demonstrate Method overloading for calculating area of
square, rectangle and triangle.

public class AreaCalculator {

// USN : 1MJ23IS026 Name: Hashith Gowda N

public double calculateArea(double side) {

return side * side;

public double calculateArea(double length, double breadth) {

return length * breadth;

// USN : 1MJ23IS026 Name: Hashith Gowda N

public double calculateArea(double base, double height, boolean isTriangle) {

if (isTriangle) {

return 0.5 * base * height;

return -1; // Return -1 if not a triangle

public static void main(String[] args) {

AreaCalculator calculator = new AreaCalculator();

double squareArea = calculator.calculateArea(5); // Side of square: 5

double rectangleArea = calculator.calculateArea(5, 10); // Length: 5, Breadth: 10

double triangleArea = calculator.calculateArea(5, 8, true); // Base: 5, Height: 8

// USN : 1MJ23IS026 Name: Hashith Gowda N

System.out.println("Area of the square: " + squareArea);

System.out.println("Area of the rectangle: " + rectangleArea);

System.out.println("Area of the triangle: " + triangleArea);

}// USN : 1MJ23IS026 Name: Hashith Gowda N


JAVA ASSIGNMENT

OUTPUT:
JAVA ASSIGNMENT

Question 17: Write a java program to demonstrate method overriding for a class Vehicle and it’s
subclass Car.

// USN : 1MJ23IS026 Name: Hashith Gowda N

class Vehicle {

// Method to be overridden

void start() {

System.out.println("Vehicle is starting...");

// USN : 1MJ23IS026 Name: Hashith Gowda N

class Car extends Vehicle {

@Override

void start() {

System.out.println("Car is starting with a key or push button...");

public class Main {

public static void main(String[] args) {

// Create an instance of Vehicle

Vehicle myVehicle = new Vehicle();

myVehicle.start(); // Calls the Vehicle's start method

Car myCar = new Car();

myCar.start(); // Calls the Car's overridden start method

// USN : 1MJ23IS026 Name: Hashith Gowda

Vehicle anotherVehicle = new Car();

anotherVehicle.start(); // Calls the Car's overridden start method

}
JAVA ASSIGNMENT

OUTPUT:
JAVA ASSIGNMENT

Question 18: Write a java program to demonstrate dynamic binding of classes A and B.

// USN : 1MJ23IS026 Name: Hashith Gowda N

class A {

void display() {

System.out.println("This is the display method in class A");

class B extends A {

@Override

void display() {

System.out.println("This is the display method in class B");

// USN : 1MJ23IS026 Name: Hashith Gowda N

public class DynamicBindingDemo {

public static void main(String[] args) {

A objA = new A();

objA.display(); // Calls display method in class A

// USN : 1MJ23IS026 Name: Hashith Gowda N

A objB = new B();

objB.display(); // Calls display method in class B (dynamic binding)

// USN : 1MJ23IS026 Name: Hashith Gowda N

B objBDirect = new B();

objBDirect.display(); // Calls display method in class B

}// USN : 1MJ23IS026 Name: Hashith Gowda N


JAVA ASSIGNMENT

OUTPUT:
JAVA ASSIGNMENT

Question 19:Write a program where user define two number and then divide it using try
block.

//USN:1MJ23IS026 Name:Hashith Gowda N

import java.util.Scanner;

import java.util.InputMismatchException;

public class Main {

public static void main(String args[]) {

double a, b;

double result;

result = 0;

Scanner in = new Scanner(System.in);

try {

System.out.println("Enter first integer : ");

a = in.nextDouble();

System.out.println("Enter Second integer : ");

b = in.nextDouble();

result = a/b;

} catch ( ArithmeticException e) {

System.out.println("Denominator cannot be zero, making it 1 !!");

b = 1;

} catch ( InputMismatchException e) {

System.out.println("Invalid input!");

e.printStackTrace();

//USN:1MJ23IS026 Name:Hashith Gowda N

System.out.println("Execution complete...");

System.out.println("Division : "+result);

}//USN:1MJ23IS026 Name:Hashith Gowda N


JAVA ASSIGNMENT

OUTPUT:

You might also like