How to fix 'cannot find symbol' in a function call? - java

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;
}
}
}

Related

myRectangle.calculateArea(); returns error "package myRectangle does not exist"

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

recursive method multiplying without using operator

import java.util.Scanner;
public class LAB1201 {
static int multi(int a, int b){
int c = 0;
if (b == 0) {
c = 0;
}
if (b < 0) {
c = (-multi(a, -b));
}
if (b > 0) {
c = (a + multi(a, b-1));
}
return c;
}
public static void main(String[]args){
int aa;
int bb;
Scanner scanner = new Scanner(System.in);
System.out.println("Type in a integer");
aa = scanner.nextInt();
System.out.println("Type in another integer");
bb = scanner.nextInt();
multi(aa,bb);
}
}
I am coding
(Write a recursive function that multiplies two numbers x and y using recursion (do not
use the multiplication operator). Your main method should prompt the user for the two
numbers, call your function, and print the result)
It allows me to type in values but I am not sure why it is not returning any values
it returns nothing..
You forgot to output the value.
System.out.println(aa + " x " + bb + " = " + multi(aa,bb));
Output example:
Type in a integer
4
Type in another integer
8
4 x 8 = 32
Your code is fine. You just forgot to print the result returned by method static int multi(int a, int b). You can find working code here
You are missing a printing statement. Here is the code, please have a look.
import java.util.Scanner;
public class so {
static int multi(int a, int b){
int c = 0;
if(b<0){
return c=(-multi(a, -b));
}
if(b>0){
return c=(a+multi(a, b-1));
}
return c;
}
public static void main(String[]args){
int aa;
int bb;
Scanner scanner = new Scanner(System.in);
System.out.println("Type in a integer");
aa = scanner.nextInt();
System.out.println("Type in another integer");
bb = scanner.nextInt();
System.out.println(multi(aa,bb));
}

Eclipse outputting strange string at end of code

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.

Java methods | Passing values

I am a beginner in Java and my professor wants us to create a program that involves some math. The thing is that he wants the calculations in a separate method. Can someone please help me. The program will not run the result for some reason. I tried looking around this website and could not find the answer. Here is the code below:
import javax.swing.JOptionPane;
public class forFun {
public static void main(String[] args)
{
double x, y, z;
String xVal, yVal, zVal;
xVal = JOptionPane.showInputDialog("Enter first integer: ");
yVal = JOptionPane.showInputDialog("Enter second integer: ");
zVal = JOptionPane.showInputDialog("Enter third integer: ");
x = Double.parseDouble(xVal);
y = Double.parseDouble(yVal);
z = Double.parseDouble(zVal);
System.exit(0);
}
public static void sumOfStocks(double x, double y, double z)
{
double result = x * y * z;
System.out.println("The product of the integers is: " + result);
System.exit(0);
}
}
Here is the example of how the code should be
import javax.swing.JOptionPane;
public class forFun {
public static void main(String[] args)
{
double x, y, z;
String xVal, yVal, zVal;
xVal = JOptionPane.showInputDialog("Enter first integer: ");
yVal = JOptionPane.showInputDialog("Enter second integer: ");
zVal = JOptionPane.showInputDialog("Enter third integer: ");
x = Double.parseDouble(xVal);
y = Double.parseDouble(yVal);
z = Double.parseDouble(zVal);
double result = sumOfStocks(x, y, z);
System.out.println("The result is %d", result);
System.exit(0);
}
public static double sumOfStocks(double x, double y, double z)
{
double result = x * y * z;
return result;
}
}

Fraction adding java program not working right

Having trouble with this final program for my Java class. We have to only use concepts we have learned so far so I cannot use other classes. Basically just loops and arrays and methods.
So for this program we have to add any five sets of fractions entered and give the GCD and the results in the lowest form. I have to show all data in the first table and then a second table with the original data and the GCD and the results in the lowest form. It has to be tested with this data:
1/4 + 1/2
2/3 + 1/3
7/8 + 1/8
2/9 + 4/27
7/25 + 2/5
Here is the code I have so far. (Be gentle, I'm still new at this)
import java.util.Scanner;
public class HelloWorld{
public static void main(String[] args) throws Exception{
int[] num1Array = new int[5];
int[] num2Array = new int[5];
int[] deno1Array = new int[5];
int[] deno2Array = new int[5];
Scanner input = new Scanner(System.in);
for(int x=0;x<5;x++) { //Get all data from user
System.out.println("Enter data for problem " + (x+1));
System.out.println("Enter numberator for fraction 1");
num1Array[x] = input.nextInt();
System.out.println("Enter denominator for fraction 1");
deno1Array[x] = input.nextInt();
System.out.println("Enter numberator for fraction 2");
num2Array[x] = input.nextInt();
System.out.println("Enter denominator for fraction 2");
deno2Array[x] = input.nextInt();
System.out.println("********************");
}
System.out.println("*****ORIGINIAL DATA ******"); //Output all entered data
System.out.println("First Fraction \t Second Fraction");
for(int y=0;y<5;y++) {
System.out.printf("%1d/%1d \t\t %1d/%1d\n", num1Array[y], deno1Array[y], num2Array[y], deno2Array[y]);
}
System.out.println("*******FRACTIONS SHOWING ADDED RESULTS*********"); //Display results
System.out.println("First Fraction \t Second Fraction GCD Results");
for(int z=0;z<5;z++){
int finalgcd = gcdfinal(num1Array[z], num2Array[z], deno1Array[z], deno2Array[z]);
int addFrac = fracAdd(num1Array[z], num2Array[z], deno1Array[z], deno2Array[z]);
System.out.printf("%1d/%1d \t\t %1d/%1d\t\t %1d \t %1d", num1Array[z], deno1Array[z], num2Array[z], deno2Array[z], finalgcd, addFrac);
System.out.println();
}
}
public static int fracAdd(int num1, int num2, int deno1, int deno2)
{
int e = lcm(deno1, deno2); //denominator
int f1 = e / deno1;
int f2 = e / deno2;
int g1 = num1 * f1;
int g2 = num2 * f2;
int adding = g1 + g2;
int k = gcd(adding, e);
int final_num = adding / k;
int final_deno = e / k;
if(final_num == final_deno){
return 1;
}
else {
return (final_num, final_deno);
}
}
public static int gcd(int a, int b) //Calculate GCD
{
while (b > 0)
{
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static int gcdfinal(int num1, int num2, int deno1, int deno2)
{
int e = lcm(deno1, deno2); //Calculate the GCD for display
int f1 = e / deno1;
int f2 = e / deno2;
int g1 = num1 * f1;
int g2 = num2 * f2;
int end = g1 + g2;
int k = gcd(end, e);
return k;
}
public static int lcm(int a, int b) //Calculate LCM
{
return a * (b / gcd(a, b));
}
}
How can I approach doing this? Am I on the right track?
Try with this, hopefully it helps you. :)
import java.util.Scanner;
public class AddingFraction {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("please enter a fraction number in a/b format: ");
String fraction1 = sc.nextLine();
System.out.println("please enter another fraction number in a/b format: ");
String fraction2 = sc.nextLine();
addFractions(fraction1, fraction2);
}
public static void addFractions(String fractionNum1, String fractionNum2) {
int numResult = 0;
String resultFraction;
String[] frcNum1 = fractionNum1.split("/");
int numerator1 = Integer.parseInt(frcNum1[0]);
int Denomenator1 = Integer.parseInt(frcNum1[1]);
String[] frcNum2 = fractionNum2.split("/");
int numerator2 = Integer.parseInt(frcNum2[0]);
int Denomenator2 = Integer.parseInt(frcNum2[1]);
if (Denomenator1 == Denomenator2) {
numResult = numerator1 + numerator2;
resultFraction = numResult + "/" + Denomenator1;
System.out.println("Resultant Fraction is : "+resultFraction);
} else {
int denLcm = Denomenator1 * (Denomenator2 / gcd(Denomenator1, Denomenator2));;
numResult = numerator1 * (denLcm / Denomenator1) + numerator2
* (denLcm / Denomenator2);
resultFraction = numResult + "/" + denLcm;
System.out.println("Resultant Fraction is : "+resultFraction);
}
}
private static int gcd(int a, int b) {
while (b > 0) {
int temp = b;
b = a % b; // % is remainder
a = temp;
}
return a;
}
}

Categories

Resources