Recently I began noticing that at the end of every code I run through Eclipse, it will end with an anomaly:
[0x7FFABD5170E3] ANOMALY: use of REX.w is meaningless (default operand size is 64)
If seeing the entire code helps, here it is:
import java.util.Scanner;
public class FracCalc {
static Scanner sc = new Scanner(System.in);
public static int inputA(){
System.out.print("Integer A: ");
int x = sc.nextInt();
return x;
}
public static int inputB(){
System.out.print("Integer B: ");
int x = sc.nextInt();
return x;
}
public static double division(double n, double d){
double a = (n / d);
return a;
}
public static void main(String[] args) {
int numerator = inputA();
System.out.println("Divided by...");
int denominator = inputB();
double answer = division(numerator, denominator);
System.out.println(answer);
}
}
If someone could help explain why this is happening that would be very appreciated.
Related
Rectangle
public class Rectangle {
private double width;
private double length;
public Rectangle(double L, double W){
length = L;
width = W;
}
public void setLength(double Length){
if (length>=0 && length <=20)
length = Length;
else{
length = 0;
}
}
public double getLength(){
return length;
}
public void setWidth(double Width){
if (width>=0 && length <=20)
width = Width;
else{
width = 0;
}
}
public double getWidth(){
return width;
}
public void calculatePerimeter(){
System.out.println("The perimeter of rectangle is: " + 2 * (length + width));
}
public void calculateArea(){
System.out.println("The area of the rectangle is: " + (length * width));
}
}
TestRectangle
import java.util.Scanner;
public class TestRectangle {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Rectangle myRectangle = new Rectangle (0,0);//I did the same thing in a
//previous assignment, and someone else who did this assignment (code found
//online) also did this. It worked before but seems useless now?
System.out.println("Enter length: ");
double L = input.nextDouble();
System.out.println();
System.out.println("Enter width: ");
double W = input.nextDouble();
System.out.println();
}
myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();//and here is where I get the error
//<identifier> expected, package myRectangle does not exist
}
I am trying to create a program "Rectangle" to calculate the area and perimeter of a rectangle, and then create a test program to run program "Rectangle"
I have copied code from a previous assignment called "Date", where the basic idea is similar, but when I get to the end of the program where I need to call on "calculateArea();" and "calculatePerimeter();" in the test program, I get an error telling me that package myRectangle doesn't exist.... can someone tell me why this is happening? A similar code worked in the previous assignment, and I found someone else's code for the same "Rectangle" program and it shows the same error. Did I do something wrong or is there something wrong with my NetBeans?
This is the code I based the Rectangle and TestRectangle program off of
Date
public class Date {
private int month;
private int day;
private int year;
public Date(int m, int d, int y){
month = m;
day = d;
year = y;
}
public void setMonth(int Months){
if(Months>=0 && Months <= 12)
month=Months;
else{
month=0;
}
}
public int getMonth(){
return month;
}
public void setDay(int Days){
if(Days>= 0 && Days<=31)
day = Days;
else{
day=0;
}
}
public int getDay(){
return day;
}
public void setYear(int Years){
year=Years;
}
public int getYear(){
return year;
}
public void displayDate(){
System.out.printf
("%d/%d/%d\n", getMonth(), getDay(), getYear() );
}
}
TestDate
import java.util.Scanner;
public class DateTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Date myDate = new Date(0,0,0);
System.out.println("Justine Dodge, assignment 6\n");
System.out.println("Please enter month: ");
int m = input.nextInt();
myDate.setMonth(m);
System.out.println();
System.out.println("Enter day: ");
int d = input.nextInt();
myDate.setDay(d);//assign d to Day?
System.out.println();//output blank line
System.out.println("Enter year: ");
int y = input.nextInt();
myDate.setYear(y);
System.out.println();
myDate.displayDate();
}
}
in the TestRectangle you are closing the main method before calling these functions
} // this should be at at the end of the main function
myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();//and here is where I get the error
i.e
myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();//and here is where I get the error
}
This will remove the compilation error. Now when you run it , you will get both area and perimeter as 0 because in the constructor , you are passing values of length and width as 0 and not really using the length and width taken as input.
To resolve this issue, insstead of passing 0,0 in constructor,
try to pass L,W in constructor like this
Rectangle myRectangle = new Rectangle (L,W);
myRectangle.getLength()); //
myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();
import java.util.Scanner;
public class TestRectangle {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
// Rectangle myRectangle = new Rectangle (0,0);//I did the same thing in a
//previous assignment, and someone else who did this assignment (code found
//online) also did this. It worked before but seems useless now?
System.out.println("Enter length: ");
double L = input.nextDouble();
System.out.println();
System.out.println("Enter width: ");
double W = input.nextDouble();
System.out.println();
Rectangle myRectangle = new Rectangle (L,W);
// System.out.println("get length " + myRectangle.getLength());
myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();//and here is where I get the error
//<identifier> expected, package myRectangle does not exist
}
}
then you will get o/p like this
I started learning JAVA a couple of days ago, so my question might be too basic.
I have created a piece of code which is as follows:
import java.util.Scanner;
public class Que01 {
public static void main(String[] args) {
int principle=acceptInt("Principle");
int roi=acceptInt("Rate Of Interest");
int years=acceptInt("Years");
float si=simpleInterest(principle,roi,years);
System.out.println("Simple Interest for given details is : "+si);
}
static int acceptInt(String s1)
{ System.out.println("Please Enter value for "+s1+" :");
Scanner sc = new Scanner(System.in);
int i= sc.nextInt();
return i;
}
static float simpleInterest(int p,int r, int yr)
{
return p*yr*r/100;
}
}
I want to know where should I write:
sc.close();
Also:
Any other suggestion to code for improvement are Welcome!
You should not close the scanner do not worry about that it will terminated after calculating the simpleInterest. When you run the program and after entering required values it will calculate and return the result and quit.
In you code 1 improvement is you should not create Scanner object again and again there should be only 1 object of Scanner throughout the life cycle.
Below is your updated code -
public class Que01 {
private static Scanner sc = null;
public static void main(String[] args) {
sc = new Scanner(System.in);
int principle=acceptInt("Principle");
int roi=acceptInt("Rate Of Interest");
int years=acceptInt("Years");
float si=simpleInterest(principle,roi,years);
System.out.println("Simple Interest for given details is : "+si);
}
static int acceptInt(String s1)
{ System.out.println("Please Enter value for "+s1+" :");
int i= sc.nextInt();
return i;
}
static float simpleInterest(int p,int r, int yr)
{
return p*yr*r/100;
}
}
Create Scanner on start of the program and use it again and again. thats it
hope this will help you.
Thanks to Vince, I was able to create a good version of my code.
and this is the answer I needed.
public class Que01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int principle=acceptInt(sc,"Principle");
int roi=acceptInt(sc,"Rate Of Interest");
int years=acceptInt(sc,"Years");
sc.close();
float si=simpleInterest(principle,roi,years);
System.out.println("Simple Interest for given details is : "+si);
}
static int acceptInt(Scanner sc,String s1)
{ System.out.println("Please Enter value for "+s1+" :");
int i= sc.nextInt();
return i;
}
static float simpleInterest(int p,int r, int yr)
{
return p*yr*r/100;
}
}```
import java.util.Scanner;
public class Que01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int principle = acceptInt("Principle", false, sc);
int roi = acceptInt("Rate Of Interest", false, sc)
int years = acceptInt("Years", true, sc);
float si = simpleInterest(principle, roi, years);
System.out.println("Simple Interest for given details is : " + si);
}
static int acceptInt(String s1, boolean closeScanner, Scanner sc) {
System.out.println("Please Enter value for " + s1 + " :");
int i = sc.nextInt();
if (closeScanner)
sc.close();
return i;
}
static float simpleInterest(int p, int r, int yr) {
return p * yr * r / 100;
}
}
i am not sure if this is a good practice of passing boolean as parameter to decide to close scanner
import java.util.Scanner;
public class Que01 {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int principle = acceptInt("Principle");
int roi = acceptInt("Rate Of Interest");
int years = acceptInt("Years");
float si = (p * yr * r/100);
System.out.println("Simple Interest for given details is : " + si);
}
static int acceptInt(String s1) {
System.out.println("Please Enter value for " + s1 + " :");
int i = sc.nextInt();
sc.close();
return i;
}
}
This is what I would say but I'm not an expert. sc.close just saves anything you did with the scanner, eg. writing to a file and saving the file. - thus in this scenario I wouldn't even use it. I also wouldn't use a method to calc the si unless you plan to use it elsewhere as it's just taking up more space
I was implementing method overloading with 2 different datatypes. This is how I ended up with the code.
But now it cannot find the symbols c and d . any help ?
import java.util.*;
import java.lang.*;
public class LargestOfTwoTest{
public static void main(String args[]) throws Exception{
Scanner scan = new Scanner(System.in);
System.out.println("Enter two numbers, and I wiil show you which one's largest!\n");
System.out.println("Enter two numbers: ");
double a = scan.nextDouble();
double b = scan.nextDouble();
if (a==(Math.floor(a))){
int c = (int) a;
}
else{
double c = a;
}
if (b==(Math.floor(b))){
int d = (int) b;
}
else {
double d = b;
}
System.out.print("Largest of the numbers is "+largest(c,d));
}
public static int largest(int x, int y){
if (x>y)
return x;
//System.out.print("Largest of the numbers is "+x);
else
return y;
//System.out.print("Largest of the numbers is "+y);
}
public static double largest(double x, double y){
if (x>y)
return x;
//System.out.print("Largest of the numbers is "+x);
else
return y;
//System.out.print("Largest of the numbers is "+y);
}
}
Shows error in this line
System.out.print("Largest of the numbers is "+largest(c,d));
..
LargestOfTwoTest.java:29: error: cannot find symbol
( c and d)
import java.math.BigDecimal;
import java.util.Scanner;
public class LargestOfTwo {
private Scanner scanner;
private Number a;
private Number b;
public static void main(String args[]) throws Exception {
LargestOfTwo app = new LargestOfTwo();
app.start();
}
private void start() {
readInputs();
Number largest = compare(a, b);
System.out.print("Largest of the numbers is: " + largest);
}
private void readInputs() {
scanner = new Scanner(System.in);
System.out.println("Enter two numbers, and I will show you which one is largest\n");
a = readInput();
b = readInput();
}
private Number readInput() {
Double d = scanner.nextDouble();
if (d == Math.floor(d)) {
return d.intValue();
} else {
return d;
}
}
private Number compare(Number x, Number y) {
if (new BigDecimal(x.floatValue()).compareTo(new BigDecimal(y.floatValue())) > 0) {
return x;
} else {
return y;
}
}
}
I have written a java code to convert decimal number to binary.Its works well but when input is 1024 or more output goes wrong.Ex: if input is 1024 output is 1410065408.
import java.util.Scanner;
public class story {
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter a positive decimal number");
binary bin=new binary();
System.out.println(bin.calculate(scan.nextInt()));
}
}
public class binary {
public long calculate(int num)
{
int i=1;
long result=0;
while(num>0)
{
result=result+(num%2)*i;
i*=10;
num/=2;
}
return result;
}
}
You are facing this issue because the datatype of i is int which is Overflowing.
The solution is to simply change the datatype of i from int to long.
Here is the corrected code snippet:
public static final class binary {
public long calculate(int num) {
long i = 1; //Note the change at this line
long result = 0;
while (num > 0) {
result = result + (num % 2) * i;
i *= 10;
num /= 2;
}
return result;
}
}
Input:
1024
Output:
10000000000
Error is in type i, it should be long.
CODE:
import java.util.Scanner;
public class story {
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter a positive decimal number");
System.out.println(calculate(scan.nextInt()));
}
public static long calculate(int num)
{
long i=1;
long result=0;
while(num>0)
{
result=result+(num%2)*i;
i*=10;
num/=2;
}
return result;
}
}
OUTPUT:
Enter a positive decimal number
1026
10000000010
The "i" variable goes past maximum integer value for inputs 1024 and above..
Refer here : Why 10000000*1000 giving 1410065408 in java?
I am trying to call a method that has a value that comes from a different method but modified. Here is the code:
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num;
System.out.println("Please enter an integer");
num = in.nextInt();
System.out.println(Multiply(num));
System.out.println(Multiply2(x));
}
public static int Multiply(int num){
int x = num*2;
return x;
}
public static int Multiply2(int x){
int val = x*2;
return val;
}
}
I know I have to declare x inside of main but then I have to initialize it but I want the value of x to equal to the one that equals to the Multiply2 method which multiplies the num from the method, Multiply, by 2. x would be equal to num multiplied by 2; How am I able to do this?
Your methods are exactly the same
The name of the variable doesn't matter. Wheter its called x or val, it makes no difference.
With that in mind, if you want x to be the value returned from your Multiply function, just store that in a variable and use it later. For instance,
int someValue = Multiply(num);
System.out.println(someValue);
System.out.println(Multiply2(someValue));
PS: By convention, we don't capitalize methods names. So they should be multiply and multiply2
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num;
System.out.println("Please enter an integer");
num = in.nextInt();
System.out.println(Multiply(num));
System.out.println(Multiply2(Multiply(num)));
}
public static int Multiply(int num) {
int x = num * 2;
return x;
}
public static int Multiply2(int x) {
int val = x * 2;
return val;
}
}