Homework LAB 9
unknown
java
2 years ago
3.7 kB
43
Indexable
//hw1
import java.util.Scanner;
public class HomeworkOne {
public static boolean isPrime(int a) {
boolean isPrime = true;
if (a <= 1) {
isPrime = false;
} else {
for (int i = 2; i < a; i++) {
if (a % i == 0) {
isPrime = false;
break;
}
}
}
return isPrime;
}
public static boolean isPerfect(int a) {
int sum = 0;
boolean isPerfect = false;
for (int i = 1; i < a; i++) {
if (a % i == 0) {
sum += i;
}
}
if (sum == a) {
isPerfect = true;
}
return isPerfect;
}
public static int special_sum(int a) {
int sum = 0;
for (int i = 2; i <= a; i++) {
if (isPrime(i) || isPerfect(i)) {
sum += i;
}
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(isPrime(15));
System.out.println(isPerfect(33));
System.out.println(special_sum(num));
}
}
//hw2
public class HomeworkTwo {
public static void showDots(int a) {
for (int i = 0; i < a; i++) {
System.out.print(".");
}
}
public static void show_palindrome(int num) {
for (int i = 1; i <= num; i++) {
System.out.print(i);
}
for (int i = num - 1; i > 0; i--) {
System.out.print(i);
}
}
public static void showDiamond(int a) {
for (int i = 1; i <= a; i++) {
showDots(a - i);
show_palindrome(i);
showDots(a - i);
System.out.println();
}
for (int i = a - 1; i > 0; i--) {
showDots(a - i);
show_palindrome(i);
showDots(a - i);
System.out.println();
}
}
public static void main(String[] args) {
showDots(5);
System.out.println();
show_palindrome(5);
System.out.println();
showDiamond(5);
}
}
//hw3
import java.util.Scanner;
public class HomeworkThree {
public static double calcTax(int age, int salary) {
double tax = 0.00;
if (age > 18 && salary > 10000) {
if (salary >= 10000 && salary <= 20000) {
tax = salary * 0.07;
}
else if (salary > 20000) {
tax = salary * 0.14;
}
}
return tax;
}
public static void calcYearlyTax() {
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
double totalTax = 0;
double monthly_tax = 0;
for (int i = 1; i <= 12; i++) {
int salary = sc.nextInt();
monthly_tax = calcTax(age, salary);
totalTax += monthly_tax;
System.out.println("Month" + i + " tax: " + monthly_tax);
}
System.out.println("Total Yearly Tax: " + totalTax);
}
public static void main(String[] args) {
System.out.println(calcTax(16,20000));
System.out.println(calcTax(20, 18000));
calcYearlyTax();
}
}
//h4
import java.util.Scanner;
public class HomeworkFour {
public static void oneToN(int one, int a) {
for (int i = 1; i <= a; i++) {
System.out.print(i + " ");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
oneToN(1, N);
}
}
Editor is loading...
Leave a Comment