Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Java Assignment

The document contains 10 Java programs covering topics like digit sum, temperature conversion, random number generation, finding negative numbers in an array, method overloading, creating and using rectangle class, inheritance, creating and using person class, finding maximum element in a 2D array, and finding number of elements greater than the average.

Uploaded by

Johan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Java Assignment

The document contains 10 Java programs covering topics like digit sum, temperature conversion, random number generation, finding negative numbers in an array, method overloading, creating and using rectangle class, inheritance, creating and using person class, finding maximum element in a 2D array, and finding number of elements greater than the average.

Uploaded by

Johan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

package Assignments;

import java.util.Scanner;

public class digitSum{

public static void main (String[] args){

Scanner sc = new Scanner(System.in);

System.out.println("Enter a Number between 0 and 1000: ");

int a = sc.nextInt();

if(a>=0&&a<=1000){

int Sum = 0;

while (a!=0){

Sum += a%10;

a = a/10;

System.out.println("Sum of digits = "+ Sum);

}else{

System.out.println("Number not in range");

sc.close();

}
2.

package Assignments;

import java.util.Scanner;

public class Temperature {

public static double celsiusToFarenheit(double celsius){

double Farenheit;

Farenheit = (9.0/5.0)*celsius+32;

return Farenheit;

public static double farenheitToCelsius(double farenheit){

double Celsius;

Celsius = (farenheit-32)*5.0/9.0;

return Celsius;

public static void main(String args[]) {

int choice;

double Temp, Result;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the temperature: ");

Temp = sc.nextDouble();

System.out.println("Make your choice: ");

System.out.println("1.Celsius to Farenheit");

System.out.println("2.Farenheit to Celsius");

choice = sc.nextInt();
switch(choice){

case 1:

Result = celsiusToFarenheit(Temp);

System.out.println("The Temperature is "+Result+"F");

break;

case 2:

Result = farenheitToCelsius(Temp);

System.out.println("The Temperature is "+Result+"C");

break;

default:

System.out.println("Invalid Choice");

sc.close();

}
3.

package Assignments;

import java.lang.Math;

public class random{

public static void main(String[] args){

double rand;

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

rand = (Math.random()*100)+1;

System.out.println((int)rand);

}
4.

package Assignments;

import java.util.Scanner;

public class Negetive{

public static void main(String[] args){

int arr[] = new int[5];

Scanner sc = new Scanner(System.in);

int count, I;

System.out.println("Enter 5 Number: ");

for(I=0;I<5;I++){

arr[I] = sc.nextInt();

count = 0;

for(I=0;I<5;I++){

if(arr[I]<0){

count++;

System.out.println("Number of Negetive Numbers: "+count);

}
5.

package Assignments;

import java.util.Scanner;

public class maxoverload{

public static int max(int a, int b){

if(a>b){

return a;

}else{

return b;

public static double max(double a, double b, double c){

double Max;

Max = a;

if(b>Max){

Max = b;

if(c>Max){

Max = c;

return Max;

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

int num1, num2;

System.out.println("Enter two Integers: ");

num1 = sc.nextInt();

num2 = sc.nextInt();

System.out.println("Max of the Integers = "+ max(num1, num2));


System.out.println("Max of the Decimals = "+max(5.6, 7.2, 8.2));

sc.close();

}
6.

package Assignments;

import java.util.Scanner;

public class Rectangle {

double width;

double height;

public Rectangle() {

width = 1.0;

height = 1.0;

public Rectangle(double width, double height) {

this.width = width;

this.height = height;

public double getArea() {

return width * height;

public double getPerimeter() {

return 2 * (width + height);

public static void main(String[] args) {

Scanner sc= new Scanner(System.in);

Rectangle rect1 = new Rectangle();

System.out.println("The area of the default rectangle is " + rect1.getArea());

System.out.println("The perimeter of the default rectangle is " + rect1.getPerimeter());


System.out.println("Enter the width and height");

int a = sc.nextInt();

int b = sc.nextInt();

Rectangle rect2 = new Rectangle(a, b);

System.out.println("The area of the rectangle with width "+ a +" and height "+ b + " is " +
rect2.getArea());

System.out.println("The perimeter of the rectangle with width "+ a +" and height "+ b + " is " +
rect2.getPerimeter());

}
7.

package Assignments;

class FromVegetable {

String color = "Red";

public String toString(){

return color;

public class Vegetable extends FromVegetable{

public String toString(){

return "Apple";

public static void main(String[] args) {

Vegetable V = new Vegetable();

FromVegetable F = new FromVegetable();

System.out.println(V.toString());

System.out.println(F.toString());

}
8.

package Assignments;

import java.util.Scanner;

public class Person {

String name;

String address;

String phoneNumber;

String emailAddress;

public Person() {

name = null;

address = null;

phoneNumber = null;

emailAddress = null;

public void Input() {

Scanner sc = new Scanner(System.in);

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

name = sc.nextLine();

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

address = sc.nextLine();

System.out.print("Enter Phone Number : ");

phoneNumber = sc.nextLine();

System.out.print("Enter Email Address : ");

emailAddress = sc.nextLine();

}
public void displayDetails() {

System.out.println("\n\n\t-----------DETAILS-----------\n");

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

System.out.println("Address: " + address);

System.out.println("Phone Number: " + phoneNumber);

System.out.println("Email Address: " + emailAddress);

public static void main(String[] args) {

Person p = new Person();

p.Input();

p.displayDetails();

}
9.

package Assignments;

import java.util.Scanner;

public class matmax{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

int mat[][] = new int[2][2];

int i, j;

System.out.println("Enter elements of 2x2 matrix: ");

for(i = 0;i<2;i++){

for(j = 0; j<2;j++){

mat[i][j] = sc.nextInt();

int Max = mat[0][0];

for(i = 0;i<2;i++){

for(j = 0; j<2;j++){

if(mat[i][j]>Max){

Max = mat[i][j];

System.out.println("Largest Number = "+Max);

}
10.

package Assignments;

import java.util.Scanner;

public class GreaterThanAverage {

public static double Average(int[] arr){

long Sum = 0;

double average;

int i;

for(i=0;i<100;i++){

Sum += arr[i];

average = Sum/100;

return average;

public static int BiggerThanAverage(int[] arr, double average){

int count, i;

count = 0;

for(i=0;i<100;i++){

if(arr[i]>average){

count++;

return count;

public static void main(String[] args) {

int arr[] = new int[100];

int Count, i;

double avg;
Scanner sc = new Scanner(System.in);

System.out.println("Enter 100 Numbers: ");

for(i=0;i<100;i++){

arr[i] = sc.nextInt();

sc.close();

avg = Average(arr);

Count = BiggerThanAverage(arr, avg);

System.out.println("The Number of Items Greater than Average("+avg+") = "+Count);

}
}

You might also like