0% found this document useful (0 votes)
12 views15 pages

Java Programs Unit 1

java programs java jajnchnhufryhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

Uploaded by

vkjanani2005
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)
12 views15 pages

Java Programs Unit 1

java programs java jajnchnhufryhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

Uploaded by

vkjanani2005
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/ 15

JAVA RECORD- UNIT 1

2513712080008 - V K JANANI
1)
package Program1;

import java. util. Scanner;

public class Arithmetic Operations {

public static void main (String [] args) {

Scanner scanner = new Scanner (System.in);

System.out.print("2513712080008- V K JANANI ");

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

int num1 = scanner. nextInt ();

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

int num2 = scanner. nextInt ();

int sum = num1 + num2;

int difference = num1 - num2;

int product = num1 * num2;

int quotient = num1 / num2;

int remainder = num1 % num2;

System.out.println("Arithmetic Operations:");

System.out.println("Addition: " + num1 + " + " + num2 + " = " + sum);

System.out.println("Subtraction: " + num1 + " - " + num2 + " = " + difference);

System.out.println("Multiplication: " + num1 + " * " + num2 + " = " + product);

System.out.println("Division: " + num1 + " / " + num2 + " = " + quotient);

System.out.println("Modulus: " + num1 + " % " + num2 + " = " + remainder);

}}

OUTPUT:
2) package Program1;

import java. util. Scanner;

public class EvenOddChecker {

public static void main (String [] args) {

Scanner scanner = new Scanner (System.in);

System.out.print("2513712080008- V K JANANI ");

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

int number = scanner. nextInt ();

if (number % 2 == 0)

System.out.println(number + " is an even number.");

else {

System.out.println(number + " is an odd number.");

}
OUTPUT:

3)

OUTPUT:

4)
PROGRAM:
package u1prg4;
import java. util. Scanner;
public class ReverseNumber {
public static void main (String [] args) {
Scanner scanner = new Scanner (System.in);
System.out.print("2513712080010,Jayasri AS \n");
System.out.print("Enter a number: ");
int number = scanner. nextInt();
int reversedNumber = 0;
while (number!= 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
System.out.println("The reversed number is: " + reversedNumber);
}
}
OUTPUT:

5)
PROGRAM:
package u1prg5;
import java. util. Scanner;

public class Factorial {

public static void main (String [] args) {


Scanner scanner = new Scanner (System.in);
System.out.print("2513712080010, Jayasri AS\n");
System.out.print("Enter a number: ");
int number = scanner. nextInt ();
int factorial = 1;
for (int i = 1; i<= number; i++) {
factorial *= i;
}
System.out.println("The factorial of " + number + " is: " + factorial);
}
}
OUTPUT:

6)
PROGRAM:
package u1prg6;
import java. util.Arrays;
import java. util. Scanner;
public class SortArray {

public static void main (String [] args) {


Scanner scanner = new Scanner (System.in);
System.out.print("2513712080010, Jayasri AS\n");
System.out.print("Enter the number of elements in the array: ");
int size = scanner. nextInt ();
int [] numbers = new int[size];
System.out.println("Enter the elements of the array:");
for (int i = 0; i< size; i++) {
numbers[i] = scanner. nextInt ();
}
Arrays.sort(numbers);
System.out.println("The sorted array is:");
for (int Num: numbers) {
System.out.print(Num + " ");
}
System.out.println();
}
}
OUTPUT:

7)
PROGRAM:
package u1prg7;
import java. util. Scanner;
public class PrimeNumber {
public static void main (String [] args) {
Scanner scanner = new Scanner (System.in);
System.out.print("2513712080010,Jayasri AS\n");
System.out.print("Enter a number: ");
int number = scanner. nextInt ();
booleanisPrime = true;
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i<= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}

OUTPUT:

8)
PROGRAM:
package u1prg8;
import java. util. Scanner;

public class GradeCalculator {


public static void main (String [] args) {
Scanner scanner = new Scanner (System.in);
System.out.print("2513712080010,Jayasri AS\n");
System.out.print("Enter the total number of subjects: ");
int NumSubjects = scanner. nextInt ();
int totalMarks = 0;
for (int i = 1; i<= NumSubjects; i++) {
System.out.print("Enter marks for subject " + i + ": ");
int marks = scanner. nextInt ();
totalMarks += marks;
}
double percentage = (double) totalMarks / (NumSubjects * 100) * 100;
char grade;
if (percentage >= 90) {
grade = 'A';
} else if (percentage >= 80) {
grade = 'B';
} else if (percentage >= 70) {
grade = 'C';
} else if (percentage >= 60) {
grade = 'D';
} else if (percentage >= 50) {
grade = 'E';
} else {
grade = 'F';
}
System.out.println("Total marks obtained: " + totalMarks);
System.out.println("Percentage: " + percentage + "%");
System.out.println("Grade: " + grade);
}
}

OUTPUT:

9)
PROGRAM:
package u1prg9;
public class Car {
// Instance variables
private String make;
private String model;
private int year;
private double fuelLevel;
// Constructor
public Car (String make, String model, int year) {
this. make = make;
this. model = model;
this. year = year;
this. fuelLevel = 100.0; // Assuming the car starts with a full tank
}
// Getter and setter methods
public String getMake () {
return make;
}
public void setMake (String make) {
this. make = make;
}
public String getModel () {
return model;
}
public void setModel (String model) {
this. model = model;
}
public int getYear () {
return year;
}
public void setYear (int year) {
this. year = year;
}
public double getFuelLevel () {
return fuelLevel;
}
public void setFuelLevel (double fuelLevel) {
this. fuelLevel = fuelLevel;
}
// Instance methods
public void start () {
System.out.println("The " + make + " " + model + " is starting.");
}
public void accelerate() {
System.out.println("The " + make + " " + model + " is accelerating.");
consumeFuel(10.0);
}

public void brake () {


System.out.println("The " + make + " " + model + " is braking.");
}
private void consumeFuel(double amount) {
fuelLevel -= amount;
if (fuelLevel< 0) {
fuelLevel = 0;
System.out.println("The " + make + " " + model + " has run out of fuel.");
}
}
public static void main (String [] args) {
// Create a new Car object
System.out.print("2513712080010,Jayasri AS\n");
Car myCar = new Car("Toyota", "Camry", 2022);
// Access and modify the car's properties
System.out.println("Make: " + myCar.getMake());
System.out.println("Model: " + myCar.getModel());
System.out.println("Year: " + myCar.getYear());
System.out.println("Fuel Level: " + myCar.getFuelLevel() + "%");
// Perform actions on the car
myCar.start();
myCar.accelerate();
myCar.brake();
// Modify the fuel level
myCar.setFuelLevel(50.0);
System.out.println("Fuel Level: " + myCar.getFuelLevel() + "%");
// Accelerate until the car runs out of fuel
while (myCar.getFuelLevel() > 0) {
myCar.accelerate();
}
}
}

OUTPUT:

10)
PROGRAM:
package u1prg10;

class Book {
String title;
String author;
int year;
// Constructor with one parameter
public Book (String title) {
this. title = title;
this. author = "Unknown";
this. year = 0;
}
// Constructor with two parameters
public Book (String title, String author) {
this. title = title;
this. author = author;
this. year = 0;
}
// Constructor with three parameters
public Book (String title, String author, int year) {
this. title = title;
this. author = author;
this. year = year;
}
// Method to display book details
public void display() {
System.out.println("Title: " + title + ", Author: " + author + ", Year: " + year);
}
public static void main(String[] args) {
System.out.print("2513712080010,Jayasri As\n");
Book book1 = new Book("The Great Gatsby");
Book book2 = new Book("1984", "George Orwell");
Book book3 = new Book("To Kill a Mockingbird", "Harper Lee", 1960);
book1.display(); // Title: The Great Gatsby, Author: Unknown, Year: 0
book2.display(); // Title: 1984, Author: GeorgeOrwell, Year: 0
book3.display(); // Title: To Kill a Mockingbird, Author: Harper Lee, Year: 1960
}
}

OUTPUT:

11)
PROGRAM:
package u1prg11;

class MathOperations {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Method to add two double values
public double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.print("2513712080010,Jayasri AS\n");
System.out.println("Sum of 5 and 10: " + math.add(5, 10)); // 15
System.out.println("Sum of 5, 10, and 15: " + math.add(5, 10, 15)); // 30
System.out.println("Sum of 5.5 and 10.5: " + math.add(5.5, 10.5)); // 16.0
}
}

OUTPUT:

You might also like