I NEED SOME TO REWRITE THE SAME PROGRMA IN ORDER TO GIVE THE SAME OUTPUT BY EITH
ID: 3548575 • Letter: I
Question
I NEED SOME TO REWRITE THE SAME PROGRMA IN ORDER TO GIVE THE SAME OUTPUT BY EITHER CHANGING THE VARIABLE OR ETC. IT CAN'T BE THE SAME PROGRAM. THIS IS VERY BASIC COMPUTER SCEINCE SO PLEASE NO COMPLICATED PROGRAMS.
COMMENT IF YOU HAVE ANY QUESTIONS.
import java.util.Scanner;
public class Assignment2 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a line of text with no puncutation.java i ");
String lineText = keyboard.nextLine( );
int position = lineText.indexOf(" ");
String sentence = lineText.substring(position+1) + " " + lineText.substring(0, position);
String newSentence = sentence.substring(0,1);
newSentence = newSentence.toUpperCase( );
sentence = newSentence + sentence.substring(1);
System.out.println("");
System.out.println("I have rephrased that line to read : " );
System.out.println(sentence);
System.out.println("");
System.out.println("Enter price of item ");
int price = keyboard.nextInt( );
System.out.println("You bought an item for " + price + " cents and gave me a dollar, so your change is:");
int change = 100-price;
int quarters = change/25;
change = change%25;
int dimes = change/10;
change = change%10;
int nickel = change/5;
System.out.println(quarters + " quarters" );
System.out.println(dimes + " dimes");
System.out.println(nickel + " nickel");
System.out.println("");
System.out.println("Enter a 4-bit binary number: ");
String binary = keyboard.next( );
String number4 = binary.substring(0,1);
String number3 = binary.substring(1,2);
String number2 = binary.substring(2,3);
String number1 = binary.substring(3);
int b4= Integer.parseInt(number4);
int b3= Integer.parseInt(number3);
int b2= Integer.parseInt(number2);
int b1= Integer.parseInt(number1);
int number = 8*b4 + 4*b3 + 2*b2 + b1;
System.out.println("");
System.out.println("After the conversion the 4 digit number to be decimal is: ");
System.out.println(number);
}
}
Explanation / Answer
Please rate me !
Code :
import java.util.Scanner;
public class Assignment2 {
public static void main(String[] args) {
// ---------------- rephrase of line starts ------------------------
Scanner input = new Scanner(System.in);
System.out.println("Enter a line of text with no puncutation in it ");
String inputText = input.nextLine();
int position = inputText.indexOf(" ");
String sentence = inputText.substring(position + 1) + " "
+ inputText.substring(0, position);
String newSentence = sentence.substring(0, 1);
newSentence = newSentence.toUpperCase();
sentence = newSentence + sentence.substring(1);
System.out.println("");
System.out.println("I have rephrased that line to read : ");
System.out.println(sentence);
System.out.println("");
// ---------------- rephrase of line ends ------------------------
// ---------------- calculate change starts ------------------------
System.out.println("Enter price of item ");
int price = input.nextInt();
System.out.println("You bought an item for " + price
+ " cents and gave me a dollar, so your change is:");
int changes = 100 - price;
int quarters = changes / 25;
changes = changes % 25;
int dimes = changes / 10;
changes = changes % 10;
int nickels = changes / 5;
System.out.println(quarters + " quarters");
System.out.println(dimes + " dimes");
System.out.println(nickels + " nickel");
System.out.println("");
// ---------------- calculate change ends ------------------------
// ---------------- 4-bit binary number starts ------------------------
System.out.println("Enter a 4-bit binary number: ");
String binaryNumber = input.next();
String fourthNumber = binaryNumber.substring(0, 1);
String thirdNumber = binaryNumber.substring(1, 2);
String secondNumber = binaryNumber.substring(2, 3);
String firstNumber = binaryNumber.substring(3);
int b4 = Integer.parseInt(fourthNumber);
int b3 = Integer.parseInt(thirdNumber);
int b2 = Integer.parseInt(secondNumber);
int b1 = Integer.parseInt(firstNumber);
int convertedNumber = 8 * b4 + 4 * b3 + 2 * b2 + b1;
System.out.println("/n");
System.out.println("After the conversion the 4 digit number to be decimal is: ");
System.out.println(convertedNumber);
// ---------------- 4-bit binary number ends ------------------------
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.