What is the output of this code in Java? - java

I've tried to run the code and I receive the error "point cannot be resolved to a variable". Why am I getting this error and please try running the code yourself and share what the output you're getting is. Thanks.
public class ReferenceMystery1 {
public static void main(String[] args) {
int a = 7;
int b = 9;
Point p1 = new Point(2,2);
Point p2 = new Point(2,2);
addToXTwice(a, p1);
System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
addToXTwice(b, p2);
System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
}
public static void addToXTwice(int a, Point b) {
a = a + a;
p1.x = a;
System.out.println(a + " " + p1.x);
}
}

import point class
import java.awt.Point;
replace
p1.x = a;
System.out.println(a + " " + p1.x);
to
b.x = a;
System.out.println(a + " " + b.x);
complete working code is
import java.awt.Point;
public class HelloWorld{
public static void main(String[] args) {
int a = 7;
int b = 9;
Point p1 = new Point(2,2);
Point p2 = new Point(2,2);
addToXTwice(a, p1);
System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
addToXTwice(b, p2);
System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
}
public static void addToXTwice(int a, Point b) {
a = a + a;
b.x = a;
System.out.println(a + " " + b.x);
}
}

"Point" class is not available for me to run your program.
Assuming its available and you are able to compile, then, as previous responses indicated...you can't access p1.x within addToXTwice() method, as the input params are 'a' and 'b'.
with "import java.awt.Point", I get the following compilation errors
javac -g ReferenceMystery1.java
ReferenceMystery1.java:18: error: cannot find symbol
p1.x = a;
^
symbol: variable p1
location: class ReferenceMystery1
ReferenceMystery1.java:19: error: cannot find symbol
System.out.println(a + " " + p1.x);
^
symbol: variable p1
location: class ReferenceMystery1
2 errors
These are in alignment with previous comments of "p1" not available from within addToXTwice() method.

Related

(Spigot 1.8) "stained_hardened_clay:14" != " stained_hardened_clay:14"

I am making a a mini game plugin, i need to replace every blocks that not a type, so i maked this :
public static void replaceBlock(String x, String y, String z, String x2, String y2, String z2, String block, String secondBlockType, String mapDict) {
String[] BlocksToReplace = mapDict.split("\\*");
System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " " + x + " " + y + " " + z + " " + x2 + " " + y2 + " " + z2);
ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
String pos1 = "/pos1 " + x + "," + y + "," + z;
String pos2 = "/pos2 " + x2 + "," + y2 + "," + z2;
Bukkit.dispatchCommand(console, pos1);
Bukkit.dispatchCommand(console, pos2);
tasak = Bukkit.getServer().getScheduler().runTaskTimer(Main.plugin, new Runnable() {
private int count = 2;
#Override
public void run() {
if(count == 0) {
for(String b : BlocksToReplace) {
if(b == block) {
System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " fdbshjfbdjshq ");
}else {
String fillcmd = "/replace " + b + " " + secondBlockType;
System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " " + fillcmd + " " + block);
Bukkit.dispatchCommand(console, fillcmd);
}
}
tasak.cancel();
} else {
count--;
}
}
}, 20, 20);
}
Map dict is => gold_block*wool:15*stained_hardened_clay:4*wool:4*stained_hardened_clay:3*stained_hardened_clay:11*wool:9*wool:11*stained_hardened_clay:9*wool:10*wool:2*stained_hardened_clay:10*stained_hardened_clay:2*stained_hardened_clay:6*diamond_block*prismarine:2*prismarine:1*melon_block*wool:5*slime*emerald_block*quartz_block*stained_hardened_clay*sandstone:2*nether_brick*wool:14*wool:13
block var is : stained_hardened_clay:14
When i call the function, it replace every blocks not all exept the block i want
Any way to fix it ?
Dont use "==" for strings. use b.equals(block). Because "==" compares the hash values of the String objects. And the equals Method compares the string itself.
More Info: https://www.java67.com/2012/11/difference-between-operator-and-equals-method-in.html

Java (What is Wrong in this Code) [duplicate]

This question already has answers here:
ArrayIndexOutOfBoundsException when launching a java program
(2 answers)
Closed 4 years ago.
I am New to Java Coding if i run below Code the Error is Coming.. Please Help
public class LeapYear {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
int prod = a * b;
int quot = a / b;
int rem = a % b;
System.out.println(a + " + " + b + " = " + sum);
System.out.println(a + " * " + b + " = " + prod);
System.out.println(a + " / " + b + " = " + quot);
System.out.println(a + " % " + b + " = " + rem);
System.out.println(a + " = " + quot + " * " + b + " + " + rem);
}
}
O/p:::
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at LeapYear.main(LeapYear.java:4)
If you're running this on netbeans, you can pass arguments into the main method by right clicking on the project, pick properties (it's at the bottom), click on the run category and then underneath Runtime Platform and Main Class is a text field to enter arguments.
If you're calling it from the command line, e.g. running javac and passing it the main class name, just add two command line arguments after the class name.

Printing a string?

I'm in my first programming class; can anyone help me understand why I can't print my last line please?
package program4;
import java.util.*;
public class Program4 {
public static void main(String[] args) {
int a, b, c, numComparisons;
String comparisons = "Comparisons for triangleType determination: ";
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 7; i++) {
}
String triangleType = "";
System.out.print("Enter 3 positive integer lengths for the sides of a "
+ "triangle:");
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
System.out.println("The input lengths are: a = " + a + ", b = " + b + ", and"
+ " c = " + c + "");
if ((a + b < c) || (b + c < a) || (a + c < b)) {
System.out.print("There is no triangle with sides " + a + ", " + b + " and "
+ "" + c + ".");
} else {
numComparisons = 1;
comparisons += "a==b";
if (a == b) {
comparisons += "(T)" + "(b==c)";
numComparisons++;
if (b == c) {
comparisons += "(T)";
triangleType = "Equilateral";
}
} else {
comparisons += "(F)";
if (a == c) {
comparisons += "(T)";
triangleType = "Isosceles";
} else {
comparisons += "b==c";
numComparisons++;
comparisons += "(F)";
if (b == c) {
triangleType = "Isosceles";
} else {
comparisons += "a==c";
numComparisons++;
comparisons += "(F)";
triangleType = "Scalene";
}
}
}
System.out.printf("" + comparisons + (""));
System.out.printf("numComparisons = " + numComparisons);
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
}
}
}
Your last line syntax is pretty messed up.
this
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
should be
System.out.println("The triangles with sides " + a + ", "
+ b + ", and " + c + ", is " + triangleType);
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
What IDE/editor are you using? It should should show you the errors here. This should be
System.out.println("The triangles with sides " + a + ", "
+ b + ", and " + c + ", is" + triangleType);
This is better

very new to the programming and at the little exercise I cannot see where I am wrong

So maybe many of you knows the exercise we need to do about learning primitives, where we need to print h3110 w0r1d 2.0 true
so mine is this;
public class main {
public static void main(String[] args) {
// H3110 w0r1d 2.0 true
byte bir = 0;
short iki = 31;
int uc = 10;
long dort = 1;
float bes = 2.0f;
char yedi = 'H';
char sekiz = 'w';
char dokuz = 'd';
char ekstra = ' ';
char ramk = 'r';
boolean on = true;
String son = (yedi + iki + uc + ekstra + sekiz + bir + ramk + dort + dokuz + ekstra + bes + ekstra + on );
System.out.println(son);
}
}
and their solution is this;
public class Main {
public static void main(String[] args) {
byte zero = 0;
short a = 3;
int b = 1;
char d = ' ';
float e = 2.0f;
boolean f = true;
String output = "H" + a + b + b + zero + d + "w" + zero + "r" + b + "d" + d + e + d + f;
System.out.println(output);
}
}
So mine is giving me boolean and float errors, but I cant see what is wrong with that primitives.
the error Im getting is this
Main.java:16: error: bad operand types for binary operator '+'
String son = (yedi + iki + uc + ekstra + sekiz + bir + ramk + dort + dokuz + ekstra + bes + ekstra + on );
^
first type: float
second type: boolean
1 error
The line:
String son = (yedi + iki + uc ...
assigns a concatenation of multiple parameters of different types, none of which is a string, into a string.
The "solution" is to start the assignment by concatenating a string to the other parameters:
String output = "H" + a + b + ...
^
which will cast the rest of them - to strings.
You can do the same with the first example by adding an empty string at the beginning:
String son = ("" + yedi + iki + uc ...
^
Side-Note: I totally agree with T.J. Crowder's comment above...
The example works because everything can be automatically converted to a String, and addition is left-associative.
The difference between a char and a String may not be obvious to you. 'C' is a char literal, while "C" is a String literal (which can contain multiple chars).
Let's go through a few steps of the string concatenation in the example, showing conceptually how the addition is performed:
"H" + a + b + b + zero + d + "w" + zero + "r" + b + "d" + d + e + d + f;
"H3" + b + b + zero + d + "w" + zero + "r" + b + "d" + d + e + d + f;
"H31" + b + zero + d + "w" + zero + "r" + b + "d" + d + e + d + f;
"H311" + zero + d + "w" + zero + "r" + b + "d" + d + e + d + f;
"H3110" + d + "w" + zero + "r" + b + "d" + d + e + d + f;
...and so on. Every addition will always be a String + some other value, which is fine.
The addition in your code, on the other hand, tries to add incompatible types like int and boolean, which doesn't fly. Also, it tries to store the result in a String variable, which is not possible, because the result is not a String.

Java: How can I make this calculus method on limits more efficient?

how can I initialize all of my variables more easier?
is there a calculus package?
is there a more efficient solution overall?
import java.util.Scanner;
public class limits {
public static void main(String[] args)
{
String introMessage = " ***Calculus: Limits***" + "\n"
+ "This application uses the method of exhaustion" + "\n"
+ "to test limits. You enter the number that x" + "\n"
+ "approches and this program will give you three" + "\n"
+ "numbers on either side of the limit showing" + "\n"
+ "closer approximations of the limit.";
System.out.println(introMessage);
System.out.println();
String polynomialMessage = "Our function: " + "\n"
+ " lim f(x) = x^2 + x + 1 = L" + "\n"
+ "x -> a";
System.out.println(polynomialMessage);
System.out.println();
System.out.println("As x approaches a, what will our limit L be?");
System.out.println();
can I initialize all of these at once?
// initialize variables
double belowAOne = 0.0;
double belowATwo = 0.0;
double belowAThree = 0.0;
double belowAFour = 0.0;
double aboveAOne = 0.0;
double aboveATwo = 0.0;
double aboveAThree = 0.0;
double aboveAFour = 0.0;
double totalBAOne = 0.0;
double totalBATwo = 0.0;
double totalBAThree = 0.0;
double totalBAFour = 0.0;
double totalAAOne = 0.0;
double totalAATwo = 0.0;
double totalAAThree = 0.0;
double totalAAFour = 0.0;
double L = 0;
// create a Scanner object named sc
Scanner sc = new Scanner(System.in);
// perform invoice calculations until choice isn't equal to "y" or "Y"
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
System.out.println("Please enter a whole number between 1 and 10 for a: ");
double a = sc.nextDouble();
if (a > 0 && a <=10 )
{
// calculate L
L = (a*a) + a + 1;
// create values that approaches a
belowAOne = a - .5;
belowATwo = a - .1;
belowAThree = a - .01;
belowAFour = a - .001;
aboveAOne = a + .5;
aboveATwo = a + .1;
aboveAThree = a + .01;
aboveAFour = a + .001;
totalBAOne = (belowAOne * belowAOne) + belowAOne + 1;
totalBATwo = (belowATwo * belowATwo) + belowATwo + 1;
totalBAThree = (belowAThree * belowAThree) + belowAThree + 1;
totalBAFour = (belowAFour * belowAFour) + belowAFour + 1;
totalAAOne = (aboveAOne * aboveAOne) + aboveAOne + 1;
totalAATwo = (aboveATwo * aboveATwo) + aboveATwo + 1;
totalAAThree = (aboveAThree * aboveAThree) + aboveAThree + 1;
totalAAFour = (aboveAFour * aboveAFour) + aboveAFour + 1;
String chart = " x " + "x^2 + x + 1" + "\n"
+ "---------+--------------" + "\n"
+ " " + belowAOne + " : " + totalBAOne + "\n"
+ " " + belowATwo + " : " + totalBATwo + "\n"
+ " " + belowAThree + " : " + totalBAThree + "\n"
+ " " + belowAFour + " : " + totalBAFour + "\n"
+ " " + " a " + " : " + "L" + "\n"
+ " " + aboveAFour + " : " + totalAAFour + "\n"
+ " " + aboveAThree + " : " + totalAAThree + "\n"
+ " " + aboveATwo + " : " + totalAATwo + "\n"
+ " " + aboveAOne + " : " + totalAAOne + "\n";
System.out.println();
System.out.println();
System.out.println();
System.out.println(chart);
System.out.println();
System.out.println("As X approaches " + a + ", our guess "
+ "for L is: " + L);
System.out.println();
// end the program
choice = "n";
}
else
{
System.out.println();
System.out.println("***Invalid Entry***");
System.out.println();
}
}
}
}
Is there a better way to write this program?
or you could use a loop instead like this:
double[] belowA = {a - .5, a - .1, a - .01, a - .001};
double[] totalBA = new double[4];
for(int i = 0; i < 4; ++i) {
totalBA[i] = (belowA[i] * belowA[i]) + belowA[i] + 1;
}
also with a StringBuilder / StringBuffer
and finally you could create a method to handle below/above in the same way without duplicated code

Categories

Resources