step by step Create a new Eclipse project named LabSLastnameFirstname All.of thi
ID: 3735158 • Letter: S
Question
step by step
Explanation / Answer
Factorial .java
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
//To read data from user
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the number : ");
int number = sc.nextInt();
int fact = factorial(number);
System.out.println("Factorial of " + number + " is: " + fact);
}
// method that takes number as argument and return factorial
static int factorial(int number) {
int fact = 1;
for (int i = 1; i <= number; i++) {
fact = fact * i;
}
return fact;
}
}
output
Please enter the number :
5
Factorial of 5 is: 120
Parking .java
public class Parking {
static double cost = 0;
public static void main(String[] args) {
System.out.println("Parking ...." + park(150));
System.out.println("Parking ...." + park(15500));
System.out.println("Parking ...." + park(2000));
System.out.println("Parking ...." + park(15444));
System.out.println("Parking ...." + park(250));
System.out.println("Parking ...." + park(-25));
System.out.println("Parking ...." + park(20));
}
static double park(int minutes) {
if (minutes <= 30) {
cost = 0;
} else if (minutes > 30 && minutes <= 60) {
cost = 2;
} else if (minutes > 60) {
double hours = minutes / 60.0;
if (hours > 24) {
cost += 24;
minutes = (int) ((hours - 24) * 60);
cost += park(minutes);
} else if (hours > 1) {
cost += 2;
minutes = (int) ((hours - 1) * 60);
cost += park(minutes);
} else {
cost += Math.ceil((double)minutes/30);
}
}
return cost;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.