In: Computer Science
(JAVA)
For your homework, I want you to create the order of your
mini-programs based on how it is listed in this assignment
description (i.e., Program 1 should be the first program
implemented, Program 2 should be the second program, etc.). All
your mini-programs are housed inside one main method. The name of
your class for this homework should be called Homework3. You will
be submitting that single Java file to this submission box. There
are a total of two mini-programs you have to implement, and each
program is worth 40 points for functioning code and correct
outputs. Altogether, the programs are 80 points in total; the
remaining 20 points reflect your programming style and
documentation.
Program 1 - Palindrome
A palindrome is a sequence of characters that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554, and 11611. Write an application that reads in a five-digit integer and determines whether it's a palindrome. If the number is not five digits long, display an error message and allow the user to enter a new value.
Sample Input Enter a number: 11611 Sample Output 11611 is a palindrome.
Sample Input Enter a number: 55953 Sample Output 55953 is not a palindrome.
Sample Input Enter a number: 1151 Enter a number: 3920 Enter a number: 12321 Sample Output 12321 is a palindrome.
Sample Input Enter a number: 116611 Enter a number: 999999 Enter a number: 99989 Sample Output 99989 is not a palindrome.
Program 2 - Printing the Decimal Equivalent of a Binary Number
Write an application that inputs an integer containing only 0s and 1s (i.e., a binary integer) and prints its decimal equivalent. [Hint: Use the remainder and division operators to pick off the binary number's, digits one at a time, from right to left. In the decimal number system, the rightmost digit has a positional value of 1 and the next digit to the left a positional value of 10, then 100, then 1000, and so on. The decimal number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the rightmost digit has a positional value of 1, the next digit to the left a positional value of 2, then 4, then 8, and so on. The decimal equivalent of binary 1101 is 1* 1 + 0 * 2 + 1 * 4 + 1 * 8, 1 + 0 + 4 + 8, or 13]
Sample Input Enter a binary number: 1101 Sample Output 13 is the decimal equivalent of 1101
Sample Input Enter a binary number: 1000110 Sample Output 70 is the decimal equivalent of 1000110
Sample Input Enter a binary number: 11111111 Sample Output 255 is the decimal equivalent of 11111111
Sample Input Enter a binary number: 1001001110 Sample Output 590 is the decimal equivalent of 1001001110
Below is the solution:
code:
import java.util.Scanner;
public class Homework3 {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in); // scanner class
int number, temp, remainder, sum =
0; // declare the variabel
// input number
System.out.print("Enter a number:
");
number = input.nextInt();
// loop until user enter the 5
digit number
while (number < 10000) { //
number is less than 5 digit long
System.out.print("Enter a number: ");
number =
input.nextInt();
}
temp = number;
// reverse the number
while (number > 0) {
remainder =
number % 10; // take remainder
sum = (sum * 10)
+ remainder;
number = number
/ 10;
}
// check for the temp not equal to
sum
if (temp != sum)
System.out.println(temp + " is not a palindrome.");
else
System.out.println(temp + " is a palindrome.");
}
}
sample input:
Enter a number: 1151
Enter a number: 3920
Enter a number: 12321
sample output:
12321 is a palindrome.
import java.util.Scanner;
public class BinaryToDecimal {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in); // scanner class
int number;// declare the
variable
int decimal = 0;
int n = 0;
// input binary number
System.out.print("Enter a binary
number: ");
number = input.nextInt();
int tempNumber = number;
while (true) {
if (number == 0)
{
break;
} else {
int temp = number % 10; // get the
remainder
decimal += temp * Math.pow(2, n); // calculate
the remainder and add to the decimal
number = number / 10;
n++;
}
}
// display
System.out.println(decimal + " is
the decimal equivalent of " + tempNumber);
}
}
sample input:
Enter a binary number: 1101
sample output:
13 is the decimal equivalent of 1101