The first name in this array is Jack.
Close
import java.util.Arrays;
public class Arrays {
public static void main(String[] args) {
String[] student_names = {"Jack", "Mike", "Katy", "Alice"};
System.out.println("The first name in this array is " + student_names[0] + ".");
}
}
Close
The first name in this array is Katy with the grade 6.
Close
public class Arrays {
public static void main(String[] args) {
String[] student_names = {"Katy", "Alice", "Jordan", "Jack", "Matt"};
int[] student_grades = {6,6,5,4,7};
System.out.println("The first name in this array is " + student_names[0] + " with the grade " + student_grades[0]);
}
}
Close
public class Arrays {
public static void main(String[] args) {
String[] cities = {"London", "Paris", "Amsterdam", "Lisbon"};
// Part A
for (int i = 0; i < cities.length; i++) {
System.out.println(cities[i]);
}
// Part B
for (int i = cities.length-1; i >= 0; i--) {
System.out.println(cities[i]);
}
}
}
Close
The sum spent on all employees in a year is 132000.
The average monthly salary of all employees is 2200.
Close
public class Arrays {
public static void main(String[] args) {
String[] employees = {"Alex", "Tom", "Scott", "Miley", "Jackson"};
int[] salaries = {2200, 1900, 3300, 2150, 1450};
int sum = 0;
int sum2 = 0;
// Part A
for (int i = 0; i < salaries.length; i++) {
sum = sum + (salaries[i] * 12);
}
System.out.println("The sum spent on all employees in a year is " + sum);
// Part B
for (int i = 0; i < salaries.length; i++) {
sum2 = sum2 + salaries[i];
}
double average = (double) sum2 / salaries.length;
System.out.println("The average monthly salary of all employees is " + average);
}
}
Close
The average tax amount is 35.22 billion dollars.
Close
public class Arrays {
public static void main(String[] args) {
String[] companies = {"Samsung", "HP", "Sony", "Bosch", "Siemens", "Apple"};
double[] incomes = {220.1, 16.7, 215.4, 67.9, 31.2, 358.4};
double sum = 0;
double tax_rate = 0;
for (int i = 0; i < incomes.length; i++) {
if (incomes[i] < 1) {
tax_rate = 0.12;
}
else if (incomes[i] >= 1 && incomes[i] < 50) {
tax_rate = 0.15;
}
else if (incomes[i] >= 50 && incomes[i] < 100) {
tax_rate = 0.2;
}
else {
tax_rate = 0.24;
}
double tax_amount = incomes[i] * tax_rate;
System.out.println(tax_amount);
sum = sum + tax_amount;
}
double average_tax = (double) sum / incomes.length;
System.out.println("The average tax rate for those companies is " + average_tax);
}
}
Close
There are 6 numbers divisible by 2 and 4 numbers divisible by 3.
Close
public class Arrays {
public static void main(String[] args) {
int[] numbers = {2,3,7,8,5,10,12,45,34,12};
int divTwo = 0;
int divThree = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 == 0) {
divTwo = divTwo + 1;
}
if (numbers[i] % 3 == 0) {
divThree = divThree + 1;
}
}
System.out.println("There are " + divTwo + " numbers divisible by 2, and " + divThree + " numbers divisible by 3.");
}
}
Close
The highest temperature of the week is 22.1°C.
The lowest temperature of the week is 14.8°C.
The average temperature of the week is 18.2°C. It was a warm week.
Close
public class Arrays {
public static void main(String[] args) {
double[] temperatures = {15.5, 18.2, 19.8, 22.1, 20.5, 16.3, 14.8};
double highest = 0;
double lowest = 100000;
for (int i = 0; i < temperatures.length; i++) {
if (temperatures[i] > highest) {
highest = temperatures[i];
}
}
for (int i = 0; i < temperatures.length; i++) {
if (temperatures[i] < lowest) {
lowest = temperatures[i];
}
}
// Part A
System.out.println("The highest temperature of the week is " + highest);
System.out.println("The lowest temperature of the week is " + lowest);
// Part B
double sum = 0;
for (int i = 0; i < temperatures.length; i++) {
sum = sum + temperatures[i];
}
double average = sum / temperatures.length;
if (average > 18) {
System.out.println("The average temperature of the week is " + average + ". It was a warm week.");
}
else {
System.out.println("The average temperature of the week is " + average + ". It was a cool week.");
}
}
}
Close
public class Arrays {
public static void main(String[] args) {
String[] products = {"Laptop", "Headphones", "Monitor", "Mouse", "Keyboard"};
double[] sales = {1200.0, 850.5, 1120.75, 750.0, 980.25};
double highestSales = 0;
String topProduct = "null";
double sum = 0;
for (int i =0; i < sales.length; i++) {
if (sales[i] > highestSales) {
highestSales = sales[i];
topProduct = products[i];
}
sum = sum + sales[i];
}
System.out.println("The product with the highest sales is " + topProduct);
System.out.println("The total sales across all products is " + sum);
}
}
Close
The student with the highest grade in Mathematics is Sue.
Total number of students with grades in Mathematics is 3.
Close
public class Arrays {
public static void main(String[] args) {
int[] grades = {5,6,4,5,7,5,4};
String[] subjects = {"English", "Mathematics", "English", "Biology", "Mathematics", "Chemistry", "Mathematics"};
String[] students = {"John", "Alice", "Mark", "Tom", "Sue", "Mike", "Sarah"};
int highest_grade = 0;
String best_student = "null";
int math_count = 0;
for (int i = 0; i < grades.length; i++) {
if (subjects[i].equals("Mathematics") && grades[i] > highest_grade) {
highest_grade = grades[i];
best_student = students[i];
}
}
for (int i = 0; i < grades.length; i++) {
if (subjects[i].equals("Mathematics")) {
math_count++;
}
}
System.out.println("The student with the highest grade in Mathematics is " + best_student + " with a grade of " + highest_grade);
System.out.println("Total number of students with grades in Mathematics is " + math_count);
}
}
Close
The average of all odd numbers from this set is 9.4.
The odd numbers have a greater average, and they differ by 0.4.
Close
public class Arrays {
public static void main(String[] args) {
int[] numbers = {2,4,5,1,8,11,13, 22,17};
int sum_odd = 0;
int count_odd = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 != 0) {
sum_odd = sum_odd + numbers[i];
count_odd++;
}
}
// Part A
double average_odd = (double) sum_odd / count_odd;
System.out.println("The average of all odd numbers from this set is " + average_odd);
// Part B
int sum_even = 0;
int count_even = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 == 0) {
sum_even = sum_even + numbers[i];
count_even++;
}
}
double average_even = (double) sum_even / count_even;
if (average_even > average_odd) {
double difference = average_even - average_odd;
System.out.println("The even numbers have a greater average, and they differ by " + difference);
}
else if (average_even < average_odd) {
double difference = average_odd - average_even;
System.out.println("The odd numbers have a greater average, and they differ by " + difference);
}
else {
System.out.println("Their averages are the same");
}
}
}
Close
Employees | Salaries ($) |
---|---|
Alex | 2200 |
Tom | 1900 |
Scott | 3300 |
Miley | 2150 |
Jackson | 1450 |
Companies | Incomes |
---|---|
Samsung | 220.1 |
HP | 16.7 |
Sony | 215.4 |
Bosch | 67.9 |
Siemens | 31.2 |
Apple | 358.4 |
Income | Tax |
---|---|
below 1 billion | 12% |
1 billion to 50 billion | 15% |
50 billion to 100 billion | 20% |
more than 100 billion | 24% |