0% found this document useful (0 votes)
11 views11 pages

Public Class Public Static: Alpha Void String Char System System System System

The document contains a series of Java programming exercises focused on various concepts such as printing alphabets, temperature conversion, multiplication tables, finding the largest number, calculating areas of shapes, determining absolute values, and checking for perfect numbers. Each exercise is accompanied by sample code and expected outputs. The exercises emphasize the use of classes, objects, method overloading, and basic control structures in Java.
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)
11 views11 pages

Public Class Public Static: Alpha Void String Char System System System System

The document contains a series of Java programming exercises focused on various concepts such as printing alphabets, temperature conversion, multiplication tables, finding the largest number, calculating areas of shapes, determining absolute values, and checking for perfect numbers. Each exercise is accompanied by sample code and expected outputs. The exercises emphasize the use of classes, objects, method overloading, and basic control structures in Java.
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/ 11

PROBLEMS FOR PRACTICE - SET III

DEVADHARSHANI A
2248327
1. Write a JAVA program to print both the upper case and lower case
alphabets.
Code:
public class Alpha{
public static void main(String args[]){
char i,j;
System.out.println("Uppercase");
for(i='A';i<='Z';i++){
System.out.print(i+" ");
}
System.out.println("\nLowercase");
for(j='a';j<='z';j++){
System.out.print(j+" ");
}
}
}
Output:

--------------------------------------------------------------------------------------------
2. Write a JAVA program to convert the temperature from
Fahrenheit to Celsius using classes and objects.
Code:
import java.util.Scanner;
class conv{
void deg(float degre){
double fahC=(degre*1.8)+32;
System.out.println("Converted temperature is: "+fahC+" F");
}
void far(float fahr){
double degC=(fahr-32)*0.56;
System.out.println("Converted tempertaure is: "+degC+" C");
}
}
public class convertion{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Enter 1 if you want to convert degree to
fahrenheit/nEnter 2 if you want to convert fahrenheit to degree");
int choice=s.nextInt();
conv obj=new conv();
if(choice==1){
System.out.println("Enter the temperature in degree: ");
float degree=s.nextFloat();
obj.deg(degree);
}
else if(choice==2){
System.out.println("Enter the temperature in fahrenheit:
");
float fah=s.nextFloat();
obj.far(fah);
}
s.close();
}
}

Output:

--------------------------------------------------------------------------------------------
3. Write a JAVA program to print the multiplication table for the
given number using classes and objects.
Code:
import java.util.Scanner;
class mul{
void calc(int num,int end)
{
for(int i=1;i<=end;i++)
{
int ans=num*i;
System.out.println(num+"*"+i+"="+ans);
}
}
}
public class tables{
public static void main(String args[]){
Scanner s =new Scanner(System.in);
System.out.println("Enter a number: ");
int a=s.nextInt();
System.out.println("Enter the upper limit up to which the
multiplication table to be printed: ");
int max=s.nextInt();
mul obj=new mul();
obj.calc(a,max);
}
}

Output:

--------------------------------------------------------------------------------------------
4. Write a JAVA program to find the largest of three numbers using
classes and objects and constructor.
Code:
import java.util.Scanner;
class large{
public large(int a,int b,int c){
if(a>b&&a>c)
{
System.out.println(a+ " is greatest");
}
else if(b>c)
{
System.out.println(b+ " is greatest");
}
else if(c>b)
{
System.out.println(c+" is greatest");
}
else
{
System.out.println("All three are equal");
}
}
}
class Largest{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Enter number1: ");
int a=s.nextInt();
System.out.println("Enter number2: ");
int b=s.nextInt();
System.out.println("Enter number3: ");
int c=s.nextInt();
large obj=new large(a,b,c);
s.close();
}
}

Output:

--------------------------------------------------------------------------------------------
5)Write a JAVA program to calculate area of different shapes
(Rectangle, Square, Circle etc.,) using method overloading.
Code:
import java.util.Scanner;
class calc{
void area(double r){
double z = 3.14 * r * r;
System.out.println("the area of the circle is "+z);
}
void area(int a,int b){
System.out.println("the area of the rectangle is "+a*b+" sq
units");
}
void area(float s){
System.out.println("the area of the square is "+s*s+" sq
units");
}
}
public class Shape{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Enter radius for circle: ");
double r=s.nextDouble();
System.out.println("Enter side1 for rectangle: ");
int a=s.nextInt();
System.out.println("Enter side2 for rectangle: ");
int b=s.nextInt();
System.out.println("Enter side for Square: ");
float c=s.nextFloat();
calc obj=new calc();
obj.area(a,b);
obj.area(c);
obj.area(r);
}
}

Output:

-------------------------------------------------------------------------------------------
6. Write a Java program to determine the Absolute value of an
integer and double using method overloading.
Code:
import java.lang.Math;
import java.util.Scanner;
class abs{
void abso(int x){
int val=Math.abs(x);
System.out.println("Absolute value: "+val);
}
void abso(double y){
double value=Math.abs(y);
System.out.println("Absolute value: "+value);
}
}
public class absolute{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Enter a integer: ");
int a=s.nextInt();
System.out.println("Enter a double: ");
double b=s.nextDouble();
abs obj=new abs();
obj.abso(a);
obj.abso(b);
}
}

Output:

--------------------------------------------------------------------------------------------
7. Write a JAVA program to find the maximum value among two and
three integers using method overloading.
Code:
import java.util.*;
class max{
void maxT(int a,int b){
if(a>b)
{
System.out.println(a+ " is greatest");
}
else if(b>a)
{
System.out.println(b+ " is greatest");
}
else
{
System.out.println("Both are equal");
}
}
void maxT(int a,int b, int c){
if(a>b&&a>c)
{
System.out.println(a+ " is greatest");
}
else if(b>c)
{
System.out.println(b+ " is greatest");
}
else if(c>b)
{
System.out.println(c+" is greatest");
}
else
{
System.out.println("All three are equal");
}
}
}
public class Maximum{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Enter number1: ");
int a=s.nextInt();
System.out.println("Enter number2: ");
int b=s.nextInt();
System.out.println("Enter number3: ");
int c=s.nextInt();
max obj=new max();
obj.maxT(a,b);
obj.maxT(a,b,c);
s.close();
}
}

Output:

--------------------------------------------------------------------------------------------
8. Write a JAVA program to check whether the given number is
Perfect Number or not using classes and objects.
Code:
//Write a JAVA program to check whether the given number is Perfect
Number or not using classes and objects.
import java.util.Scanner;
class PerfectnumG {

void func(int n) {
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum = sum + i;
}
}
if (sum == n) {
System.out.println(n + " is a perfect number");
}
else{
System.out.println(n + " is not a perfect number");
}
}
}

public class PerfectG {


public static void main(String args[]) {

Scanner s = new Scanner(System.in);


System.out.println("Enter a number: ");
int numb = s.nextInt();
PerfectnumG obj = new PerfectnumG();
obj.func(numb);
}
}

Output:

--------------------------------------------------------------------------------------------
9. Write a JAVA program to determine whether the given year is leap
or not using Boolean variables.
Code:
//Write a JAVA program to determine whether the given year is leap or
not using Boolean variables.
import java.util.Scanner;
class Lp{
Boolean leap(int year){
if ((year%400==0)||(year%100!=0)&&(year%4==0))
{
return true;
}
else{
return false;
}
}
}
class LeapYearB
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the year");
int year=s.nextInt();
Lp obj=new Lp();
Boolean ans=obj.leap(year);
if(ans==true){
System.out.println(year+ " is a leap year");
}
else{
System.out.println("It is not a leap year");
}
s.close();
}
}

Output:

--------------------------------------------------------------------------------------------
10. Write a JAVA program to check whether the given number is odd
or even without using modulus operator.
Code:
import java.util.Scanner;
class OE{
Boolean func(int n){
if ((n / 2) * 2 == n)
{
return true;
}
else{
return false;
}
}
}
class OddEven
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the number");
int num=s.nextInt();
OE obj=new OE();
Boolean ans=obj.func(num);
if(ans==true){
System.out.println(num+ " is even");
}
else{
System.out.println("It is not even");
}
s.close();
}
}

Output:

-----------------------------------------------------------------------------------------
11. Write a program to swap or exchange two numbers.
Code:
import java.util.Scanner;
class Swap{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Enter x: ");
int x=s.nextInt();
System.out.println("Enter y: ");
int y=s.nextInt();
x=x+y;
y=x-y;
x=x-y;
System.out.println("New x: "+x);
System.out.println("New y: "+y);
}
}

Output:

________________________________________________________

You might also like