Welcome to my Java program!
Close
public class Methods {
public static void printIntroduction() {
System.out.println("Welcome to my Java program!");
}
public static void main(String[] args) {
printIntroduction();
}
}
Close
Assuming the user inputs the name "Jack", the output should be:
Hello Jack! Welcome to my Java program.
Close
import java.util.Scanner;
public class Methods {
public static void greetUser(String name) {
System.out.println("Hello, " + name + "! Welcome to my Java program.");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
greetUser(name);
}
}
Close
import java.util.Scanner;
public class Methods {
public static void isEven(int number) {
if (number % 2 == 0) {
System.out.println("The number " + number + " is even.");
}
else {
System.out.println("The number is odd");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
isEven(number);
}
}
Close
import java.util.Scanner;
public class Methods {
public static void areaCalculator(int height, int base) {
double area = (double) height * base / 2;
if (height <=0 || base <= 0) {
System.out.println("Enter valid dimensions");
}
else {
System.out.println("The area of this triangle is " + area);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the height: ");
int height = scanner.nextInt();
System.out.print("Enter the base: ");
int base = scanner.nextInt();
areaCalculator(height, base);
}
}
Close
import java.util.Scanner;
public class Methods {
public static void findMax(int number1, int number2, int number3) {
int max = number1;
if (number2 > max) {
max = number2;
}
if (number3 > max) {
max = number3;
}
if (number1 == number2 && number2 == number3) {
System.out.println("All numbers are equal.");
}
else {
System.out.println("The largest number is " + max);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int number1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int number2 = scanner.nextInt();
System.out.print("Enter the third number: ");
int number3 = scanner.nextInt();
findMax(number1, number2, number3);
}
}
Close
import java.util.Scanner;
public class Methods {
public static void countEvenNumbers(int n) {
int count = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
count++;
}
}
System.out.println("The count of all even numbers from 1 to " + n + " is " + count);
}
public static void squareNumber(int n) {
int square = n * n;
System.out.println("The square of number " + n + " is " + square);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int n = scanner.nextInt();
if (n <= 0) {
System.out.println("Please enter a positive integer.");
}
else {
countEvenNumbers(n);
squareNumber(n);
}
}
}
Close
Assuming the user inputs "full-time" and 10, the output should be:
The total monthly salary for this full-time employee is 1760.
Close
import java.util.Scanner;
public class Methods {
public static void calculateBonus(String employment_type, double wage) {
double salary = 0;
if (employment_type.equals("full-time")) {
double new_wage = wage * 1.1;
salary = new_wage * 160;
}
else {
double new_wage = wage * 1.05;
salary = new_wage * 160;
}
System.out.println("Thte total monthly salary for " + employment_type + " with initial wages of " + wage + " is " + salary);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your employment-type: ");
String type = scanner.nextLine();
System.out.print("Enter your wage: ");
double wage = scanner.nextDouble();
if (!type.equals("part-time") && !type.equals("full-time") || wage <= 0) {
System.out.println("Enter valid credentials.");
}
else {
calculateBonus(type, wage);
}
}
}
Close
Assuming the user inputs the numbers 4 and 4, the output should be:
The following dimensions result in a square: true.
Close
import java.util.Scanner;
public class Methods {
public static boolean isSquare(int height, int length) {
if (height == length) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the height: ");
int height = scanner.nextInt();
System.out.print("Enter the length: ");
int length = scanner.nextInt();
boolean result = isSquare(height, length);
System.out.println("The following dimensions result in a square: " + result);
}
}
Close
Mark received distinction with a grade of 6.3
Tom received distinction with a grade of 6.6
Sarah received distinction with a grade of 5.9
Close
import java.util.Scanner;
public class Methods {
public static boolean gotDistinction(double grade) {
if (grade > 5.8) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
double[] grade_averages = {5.2, 4.3, 6.3, 6.6, 5.1, 4.8, 5.9};
String[] students = {"John", "Alice", "Mark", "Tom", "Sue", "Mike", "Sarah"};
for (int i = 0; i < grade_averages.length; i++) {
boolean distinction = gotDistinction(grade_averages[i]);
if (distinction) {
System.out.println(students[i] + " received distinction with a grade of " + grade_averages[i]);
}
}
}
}
Close
Assuming the user inputs "addition" and the numbers 5 and 2, the output should be:
The result of the addition is 7.
Close
import java.util.Scanner;
public class Methods {
public static int addition(int number1, int number2) {
int result = number1 + number2;
return result;
}
public static int subtraction(int number1, int number2) {
int result = number1 - number2;
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the operation: ");
String operation = scanner.nextLine();
if (operation.equals("addition")) {
System.out.print("Enter the first number: ");
int number1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int number2 = scanner.nextInt();
int result = addition(number1, number2);
System.out.println("The result of the addition is " + result);
}
else if (operation.equals("subtraction")) {
System.out.print("Enter the first number: ");
int number1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int number2 = scanner.nextInt();
int result = subtraction(number1, number2);
System.out.println("The result of the subtraction is " + result);
}
else {
System.out.println("Wrong operation. Only addition and subtraction are possible.");
}
}
}
Close