I've been a lot of trouble figuring this class problem. My due date is tomorrow and I still don't know how to do it. I made a code when the input put by the user is converted into binary, octal, and hexadecimal. Now, the problem is that now they are asking me to modify the code in a way that the user inputs a Double or Floating point and convert it into decimal. My greatest problem is working with the decimal numbers; for example, 5*.987*. I will leave the code I already created. I would be very grateful is someone could help! thanks :)
import java.util.Scanner;
class EncodingTester {
public static void main(String args[]) {
byte largestPositiveByte = 127;
short largestPositiveShort = 32767;
int largestPositiveInt = 2147483647;
long largestPositiveLong = 9223372036854775807L;
long largestPositiveLongPlusOne = 9223372036854775807L;
Scanner in = new Scanner(System.in);
System.out.println("Next number (0 to stop): ");
long nextNumber = in.nextLong();
while (nextNumber != 0) {
int radix;
double logBase2 = Math.log(nextNumber) / Math.log(2);
double absoluteNumOfBits = 1 + Math.floor(logBase2);
double maxBits = Math.max(8, absoluteNumOfBits);
double numBits = Math.log(maxBits) / Math.log(2);
int newNumBits = (int) Math.pow(2, Math.ceil(numBits));
System.out.println("The absolute number of bits is: " +absoluteNumOfBits +". The final number of bits is: " +newNumBits +".");
radix = 10;
System.out.println("Decimal: " + String.format("%s", Double.toString(nextNumber)).replace(' ','0'));
radix = 2;
System.out.println("Binary: " + String.format("%"+newNumBits+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
radix = 8;
System.out.println("Octal: " + String.format("%"+((int) Math.ceil(newNumBits/3.0))+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
radix = 16;
System.out.println("Hexadecimal: 0x" + String.format("%"+newNumBits/4+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
System.out.println("");
System.out.println("Next number: (0 to stop)");
nextNumber = in.nextLong();
}
System.out.println("Good Bye");
}
}
Related
I am trying to get 3 numbers separated by a space after user's input. I can get the first number and the last one dividing by 10, but I really have no idea how to get the middle number
I tried to take the remainder of the first two numbers and then divide them by ten, but IDEA says that the answer is always zero
public static void main(String[] args) {
System.out.println("Input the number");
int number = read.nextInt();
int a = number%10;
int b = (number%10)/10; // the answer is always 0
int c = number / 100;
System.out.println(c + " " + b + " " + a);
}
Anything modulo 10 will return a result in the range of 0 to 9, and (integer) dividing that by 10 will return 0. You need to reverse the order - first divide the 10 to remove the last digit, and then take the remainder from 10 to keep the middle digit:
int b = (number / 10) % 10;
I suggest using this instead of %. Because you can split and get any digit of number using this code:
int x=158;
char[] xValueInString = Integer.toString(x).toCharArray();
for(int i=0; i<xValueInString.length; i++){
System.out.println(xValueInString[i]);
}
You missed scanner in your code, add Scanner before reading number, try this with little change
public static void main(String[] args) {
System.out.println("Input the number");
Scanner read = new Scanner(System.in);
int number = read.nextInt();
int a = number%10;
int b = (number/10)%10; // the answer is always 0
int c = number/100;
System.out.println(c + " " + b + " " + a);
}
I am attempting to write a program that will list the amount of kilograms, grams, and milligrams in a given input. Ex. in the given input of 1050042mg, the output will say that there is 1 kilogram, 50 grams, and 42 milligrams.
import java.util.Scanner;
class milligrams {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int milligram;
double kilo, gram;
System.out.println("Enter the weight in milligrams: ");
milligram = in.nextInt();
kilo = milligram / 1000000;
gram = milligram / 10000;
milligram = milligram / 1;
System.out.println("The weight is " + kilo + "kilos, " + gram + "grams" + milligram + " milligrams");
}
}
The part I am struggling with is I believe I must change the 10-12 lines with code that will read the given user input and then divide the input by the appropriate number to get kg, g, and mg but I cannot figure out how to do it as I am new to java. I am aware the division numbers are incorrect but I don't believe that is the issue. If this is not the right approach please guide me to the right approach.
You can use the modulus (clock arithmetic) operator
const KG = 1000000;
const GRAM = 1000;
Scanner in = new Scanner(System.in);
int milligram;
double kilo, gram;
System.out.println("Enter the weight in milligrams: ");
milligram = in.nextInt();
kilo = milligram / KG;
//use the modulus here (and below)
milligram = milligram % KG;
gram = milligram / GRAM;
// 2nd use of modulus, this time with GRAN
milligram = milligram % GRAM;
System.out.println("The weight is " + kilo + "kilos, " + gram + "grams" + milligram + " milligrams");
}
I have been tasked with the assignment of creating a method that will take the 3 digit int input by the user and output its reverse (123 - 321). I am not allowed to convert the int to a string or I will lose points, I also am not allowed to print anywhere other than main.
public class Lab01
{
public int sumTheDigits(int num)
{
int sum = 0;
while(num > 0)
{
sum = sum + num % 10;
num = num/10;
}
return sum;
}
public int reverseTheOrder(int reverse)
{
return reverse;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Lab01 lab = new Lab01();
System.out.println("Enter a three digit number: ");
int theNum = input.nextInt();
int theSum = lab.sumTheDigits(theNum);
int theReverse = lab.reverseTheOrder(theSum);
System.out.println("The sum of the digits of " + theNum + " is " + theSum);
}
You need to use the following.
% the remainder operator
/ the division operator
* multiplication.
+ addition
Say you have a number 987
n = 987
r = n % 10 = 7 remainder when dividing by 10
n = n/10 = 98 integer division
Now repeat with n until n = 0, keeping track of r.
Once you understand this you can experiment (perhaps on paper first) to see how
to put them back in reverse order (using the last two operators). But remember that numbers ending in 0 like 980 will become 89 since leading 0's are dropped.
You can use below method to calculate reverse of a number.
public int reverseTheOrder(int reverse){
int result = 0;
while(reverse != 0){
int rem = reverse%10;
result = (result *10) + rem;
reverse /= 10;
}
return result;
}
I need to print the circumference with Math.random() * Math.Pi; but i'm doing something wrong or missing something. Each random generated number equals the radius of the circle. My idea was to calculate Pi in the getRandomNumberInRange method but when I do, I get error:
Bad operand for type double
import java.util.Random;
import java.util.Scanner;
final static double PI = 3.141592564;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
//ask the player to enter a number less than or equal to 18 and higher to 9.
System.out.println(" Please enter a number less than or equal to 18 and above 9: ");
int random = sc.nextInt ();
//send error message if bad input
if (random < 9 || random > 18) {
System.out.println(" Error. Unauthorized entry . You need to enter a number less than or equal to 18 and above 9 ");
} else
//If the answer is yes , generate nine different random numbers from 0.
for (int i = 0; i < 9; i++) {
double surface = PI * (random * 2);
System.out.println(getRandomNumberInRange(9, 18) + " : " + " The circumference is : " + surface );
}}
The method called:
private static int getRandomNumberInRange(int min, int max) {
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
You call getRandomNumberInRange() in the for loop, but don't assign it to anything, or use it.
This is probably closer to what you want:
for (int i = 0; i < 9; i++) {
int r2 = getRandomNumberInRange(9, 18);
double surface = PI * (r2 * 2);
System.out.println(r2 + " : " + " The circumference is : " + surface);
}
I'm having trouble getting the number of digits to the left of the decimal place. I've got the digits to the right of the decimal point working and able to print out but not to the left. Can anyone help?
import java.lang.Math;
import java.util.Scanner;
import java.text.DecimalFormat;
public class FormulaCalculation
{
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
double x;
//Prompt user for value.
System.out.print("Enter a value for x: ");
x = userInput.nextDouble();
double result = (Math.sqrt(7 * Math.pow(x, 4) - 5 * Math.pow(x, 2) + Math.abs(98 * x)) + 13) * (3 * Math.pow(x, 5) + 4 * Math.pow(x, 3) + 1);
String resultString = Double.toString(result);
int integerPlaces = resultString.indexOf('.');
int digitsLeft = resultString.length();
int digitsRight = resultString.length() - integerPlaces - 1;
//Output
System.out.println("Result: " + result);
System.out.println("# digits to left of the decimal point: " + digitsLeft);
System.out.println("# digits to right of the decimal point: " + digitsRight);
System.out.println("Formatted Result: ");
}
}
Either print integerPlaces (which is the number you seek) or assign it to digitsLeft. Something like this,
int integerPlaces = resultString.indexOf('.');
int digitsLeft = integerPlaces; // <-- from 0 to the '.'
// resultString.length();
int digitsRight = resultString.length() - integerPlaces - 1;
or eliminate digitsLeft altogether,
System.out.println("# digits to left of the decimal point: " + integerPlaces);