Question

In: Computer Science

(JAVA) For your homework, I want you to create the order of your mini-programs based on...

(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

Solutions

Expert Solution

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


Related Solutions

TrackMinMax i want the generic version based on java For this lab, you will create a...
TrackMinMax i want the generic version based on java For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
Ans In Java Please Homework 6-1 So far in this course, all of your programs have...
Ans In Java Please Homework 6-1 So far in this course, all of your programs have been written to be fully contained inside a single method named main. The main method is where Java begins execution of your program. In this assignment, you will coding other methods in addition to the main method. These additional methods will perform specific functions and, in most cases, return results. Write all your methods in a class named Homework6Methods.java Write a public static method...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
(JAVA) Create a program that creates a mini database of numbers that allows the user to:...
(JAVA) Create a program that creates a mini database of numbers that allows the user to: reset the database, print the database, add a number to the database, find the sum of the elements in the database, or quit. In main, you will declare an array of 10 integers (this is a requirement). Then you will define the following methods: • printArray (int[ ] arr) – this takes in an array and prints it • initArray (int[ ] arr) –...
for java Welcome to a classic homework problem! Create a public class called Last8. You should...
for java Welcome to a classic homework problem! Create a public class called Last8. You should exposed two public methods: add: adds a value, does not return a value last: returns an array containing the last 8 values that were added, in any order. You do not need a constructor, but you can add an empty one if you need. Until 8 values have been added you should return 0s in their place. For example, here's how an instance of...
JAVA Homework 1) Create a die class. This is similar to the coin class , but...
JAVA Homework 1) Create a die class. This is similar to the coin class , but instead of face having value 0 or 1, it now has value 1,2,3,4,5, or 6. Also instead of having a method called flip, name it roll (you flip a coin but roll a die). You will NOT have a method called isHeads, but you will need a method (call it getFace ) which returns the face value of the die. Altogether you will have...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a...
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a message if they try to divide by 0. I have this so far. What code should I put? divide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (number1.getText().length() != 0 && number2.getText().length() != 0) { double n1= Double.parseDouble(number1.getText().toString()); double n2= Double.parseDouble(number2.getText().toString()); double res= n1 / n2; result.setText(String.valueOf(res)); } else { Toast.makeText(view.getContext(), "Please enter the numbers properly", Toast.LENGTH_SHORT).show(); } } });
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT