Java Assignment
Java Assignment
package Assignments;
import java.util.Scanner;
int a = sc.nextInt();
if(a>=0&&a<=1000){
int Sum = 0;
while (a!=0){
Sum += a%10;
a = a/10;
}else{
sc.close();
}
2.
package Assignments;
import java.util.Scanner;
double Farenheit;
Farenheit = (9.0/5.0)*celsius+32;
return Farenheit;
double Celsius;
Celsius = (farenheit-32)*5.0/9.0;
return Celsius;
int choice;
Temp = sc.nextDouble();
System.out.println("1.Celsius to Farenheit");
System.out.println("2.Farenheit to Celsius");
choice = sc.nextInt();
switch(choice){
case 1:
Result = celsiusToFarenheit(Temp);
break;
case 2:
Result = farenheitToCelsius(Temp);
break;
default:
System.out.println("Invalid Choice");
sc.close();
}
3.
package Assignments;
import java.lang.Math;
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;
int count, I;
for(I=0;I<5;I++){
arr[I] = sc.nextInt();
count = 0;
for(I=0;I<5;I++){
if(arr[I]<0){
count++;
}
5.
package Assignments;
import java.util.Scanner;
if(a>b){
return a;
}else{
return b;
double Max;
Max = a;
if(b>Max){
Max = b;
if(c>Max){
Max = c;
return Max;
num1 = sc.nextInt();
num2 = sc.nextInt();
sc.close();
}
6.
package Assignments;
import java.util.Scanner;
double width;
double height;
public Rectangle() {
width = 1.0;
height = 1.0;
this.width = width;
this.height = height;
int a = sc.nextInt();
int b = sc.nextInt();
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 {
return color;
return "Apple";
System.out.println(V.toString());
System.out.println(F.toString());
}
8.
package Assignments;
import java.util.Scanner;
String name;
String address;
String phoneNumber;
String emailAddress;
public Person() {
name = null;
address = null;
phoneNumber = null;
emailAddress = null;
name = sc.nextLine();
address = sc.nextLine();
phoneNumber = sc.nextLine();
emailAddress = sc.nextLine();
}
public void displayDetails() {
System.out.println("\n\n\t-----------DETAILS-----------\n");
p.Input();
p.displayDetails();
}
9.
package Assignments;
import java.util.Scanner;
int i, j;
for(i = 0;i<2;i++){
for(j = 0; j<2;j++){
mat[i][j] = sc.nextInt();
for(i = 0;i<2;i++){
for(j = 0; j<2;j++){
if(mat[i][j]>Max){
Max = mat[i][j];
}
10.
package Assignments;
import java.util.Scanner;
long Sum = 0;
double average;
int i;
for(i=0;i<100;i++){
Sum += arr[i];
average = Sum/100;
return average;
int count, i;
count = 0;
for(i=0;i<100;i++){
if(arr[i]>average){
count++;
return count;
int Count, i;
double avg;
Scanner sc = new Scanner(System.in);
for(i=0;i<100;i++){
arr[i] = sc.nextInt();
sc.close();
avg = Average(arr);
}
}