0% found this document useful (0 votes)
3 views3 pages

Java Programs Full Explained

The document contains multiple Java programs that perform various tasks such as converting Fahrenheit to Celsius, converting minutes into years and days, performing arithmetic operations, calculating the area of a rectangle, managing student information, and calculating the average of three numbers. Each program utilizes the Scanner class for user input and demonstrates basic programming concepts. The examples are structured with a main class and methods for specific functionalities.

Uploaded by

shanu7659778
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)
3 views3 pages

Java Programs Full Explained

The document contains multiple Java programs that perform various tasks such as converting Fahrenheit to Celsius, converting minutes into years and days, performing arithmetic operations, calculating the area of a rectangle, managing student information, and calculating the average of three numbers. Each program utilizes the Scanner class for user input and demonstrates basic programming concepts. The examples are structured with a main class and methods for specific functionalities.

Uploaded by

shanu7659778
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/ 3

1.

Convert Fahrenheit to Celsius

import java.util.Scanner;

public class TemperatureConverter {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter temperature in Fahrenheit: ");


double fahrenheit = sc.nextDouble();

double celsius = (fahrenheit - 32) * 5 / 9;

System.out.println("Temperature in Celsius: " + celsius);


}
}

2. Convert Minutes into Years and Days

import java.util.Scanner;

public class MinutesConverter {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter number of minutes: ");


int minutes = sc.nextInt();

int years = minutes / (60 * 24 * 365);


int remainingMinutes = minutes % (60 * 24 * 365);
int days = remainingMinutes / (60 * 24);

System.out.println("Approx: " + years + " year(s) and " + days + " day(s)");
}
}

3. Arithmetic Operations

import java.util.Scanner;

public class ArithmeticOperations {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

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


int a = sc.nextInt();

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


int b = sc.nextInt();

System.out.println("Sum: " + (a + b));


System.out.println("Difference: " + (a - b));
System.out.println("Product: " + (a * b));
System.out.println("Quotient: " + (a / b));
System.out.println("Remainder: " + (a % b));
}
}
4. Area of Rectangle Using Class

import java.util.Scanner;

class Area {
int length, breadth;

void setDim(int l, int b) {


length = l;
breadth = b;
}

int getArea() {
return length * breadth;
}
}

public class RectangleArea {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Area rect = new Area();

System.out.print("Enter length: ");


int l = sc.nextInt();
System.out.print("Enter breadth: ");
int b = sc.nextInt();

rect.setDim(l, b);
int area = rect.getArea();

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


}
}

5. Student Class with Input

import java.util.Scanner;

class Student {
String name;
int roll_no;
double YGPA;

void inputData() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
name = sc.nextLine();

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


roll_no = sc.nextInt();

System.out.print("Enter YGPA: ");


YGPA = sc.nextDouble();
}

void displayData() {
System.out.println("Name: " + name);
System.out.println("Roll No: " + roll_no);
System.out.println("YGPA: " + YGPA);
}
}

public class StudentInfo {


public static void main(String[] args) {
Student s = new Student();
s.inputData();
s.displayData();
}
}

6. Average of Three Numbers

import java.util.Scanner;

class Average {
void calculateAverage() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
System.out.print("Enter third number: ");
int c = sc.nextInt();

double avg = (a + b + c) / 3.0;


System.out.println("Average: " + avg);
}
}

public class AverageMain {


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

You might also like