1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class AgeNextYear {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("How old are you now? ");
int age = parseInt(scanner.nextLine());
System.out.println("Next year, you will be: " + (age + 1));
}
}
import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class AscendingOrder {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter three numbers: ");
ArrayList<Integer> nums = new ArrayList<Integer>();
int numOne = parseInt(scanner.nextLine());
nums.add(numOne);
int numTwo = parseInt(scanner.nextLine());
nums.add(numTwo);
int numThree = parseInt(scanner.nextLine());
nums.add(numThree);
System.out.print("Here they are in order: ");
while (nums.size() > 1) {
int min = nums.get(0);
int loc = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums.get(i) < min) {
min = nums.get(i);
loc = i;
}
System.out.print(nums.get(loc) + " ");
nums.remove(loc);
}
}
System.out.print(nums.get(0));
}
}
import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class AscendingOrder {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("How many numbers do you have? ");
int totalNums = parseInt(scanner.nextLine());
ArrayList<Integer> nums = new ArrayList<Integer>();
System.out.println("Enter your " + totalNums + " numbers now: ");
for (int i = 0; i < totalNums; i++) {
int num = parseInt(scanner.nextLine());
nums.add(num);
}
int min = nums.get(0);
int max = nums.get(0);
for (int i = 1; i < nums.size(); i++) {
if (nums.get(i) < min) {
min = nums.get(i);
}
if (nums.get(i) > max) {
max = nums.get(i);
}
}
System.out.println("The smallest number you entered was " + min);
System.out.println("The largest number you entered was " + max);
}
}
I would use a value of 2 or larger for `totalNums` for the sorting to actually be tested, and I would make sure the numbers I input are not equal to each other. I would do it again with a different number as `totalNums`, and if it works, I would consider the program to be working properly with the assumption that the program is given appropriate inputs.
import java.util.Scanner;
public class PalindromeChecker {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = scanner.nextLine().toLowerCase();
boolean isPalindrome = true;
for(int i = 0; i < word.length(); i++)
{
if(word.charAt(i) != word.charAt(word.length()-1-i)) {
isPalindrome = false;
break;
}
}
if(isPalindrome) {
System.out.println("That is a palindrome.");
} else {
System.out.println("That is not a palindrome.");
}
}
}
1.
public double transform(double num) {
return Math.sqrt(Math.abs(num)) + (Math.pow(num, 3) * 5);
}
2.
import java.util.Scanner;
import java.lang.Math;
import static java.lang.Double.parseDouble;
public class AscendingOrder {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter 5 values: ");
double[] nums = new double[5];
for (int i = 0; i < 5; i++) {
double num = parseDouble(scanner.nextLine());
nums[i] = num;
}
for (int i = 0; i < 5; i++) {
nums[i] = transform(nums[i]);
}
System.out.print("Results in reverse order: ");
for (int i = 4; i >= 0; i--) {
System.out.print(nums[i] + " ");
}
}
}
|