// This application displays some math facts
public class DebugThree2 {
public static void main(String args[]) {
add();
subtract();
System.out.println("Java");
int a = 2, b = 5, c = 10;
add("a" + "b");
add("b" + "c");
subtract("c" - "a");
}
public static void add() {
System.out.println("The sum of " + "a" + "and" + "b" + "is" + "a" + "b");
}
public static void subtract() {
System.out.println("The difference between " + "a" + "and" + "b" + "is" + "a" - "b");
}
}
I keep getting the errors method add in class DebugThree2 cannot be applied to given types, bad operand types for binary operator '-'
public static void main(String args[])
{
add();
subtract();
System.out.println("Java");
int a = 2, b = 5, c = 10;
add(a,b);
add(b,c);
subtract(c,a);
}
public static void add(int a, int b)
{
System.out.println("The sum of " + a +
" and " + b + " is " + (a+b));
}
public static void subtract(int a, int b)
{
System.out.println("The difference between " +
a + " and " + b + " is " + (a-b));
}
I think something like that may be more what you are looking for. You are trying to subtract (string)b from (string)a.
"a" - "b"
You must use their actual value, by not including quotation marks.
Related
Hi I'm petty new to java and I have a question,
I'm trying to replace the String " 22(S)" with " 22(I)" but for some reason the replace first doesn't replace the String.
Here is my code:
public class tes {
public static void main(String[] args) {
int y = 22;
String x = " 22(S)";
x = x.replaceFirst(" " + y + "(S)", " " + y + "(I)");
System.out.println(x);
}
}
While I know that I can do x.replaceFirst("S","I") , I want to understand why this is still producing 22(S) with my current code. Thanks.
Because ( and ) are grouping operators in regular expression. You need to escape them in the match term. Like,
x = x.replaceFirst(" " + y + "\\(S\\)", " " + y + "(I)");
And I get
22(I)
with no other changes.
public class Replace {
public static void main(String[] args) {
int y = 22;
String x = " 22(S)";
x = x.replaceAll(" " + y + "\\(S\\)", " " + y + "(I)");
System.out.println(x);
}
}
By using replaceAll also we can replace all the S letters to I
Suppose I wanna swap variables with eachother. Which of the solutions below would be the faster and requires less memory? Note: there is only 2 variables to be swapped here, but imagined we had one billion variables to be swapped.
Solution 1:
public class Exc15 {
public static void main(String[] args) {
int c = 1;
int d = 2;
int e = c;
System.out.println(c + " " + d);
c = d;
d = e;
System.out.println(c + " " + d);
}
}
Solution 2:
public class test {
public static void main(String[] args) {
int a = 3;
int b = 4;
System.out.println(a + " " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println(a + " " + b);
}
}
I am trying to create a method to increase the value of various variables, here is an example of the type of logic i'm currently using, however when the method is finished the original variable has not changed. What do I need to add or replace to allow the value to change outside the method?
static int num = 2;
static String text = "3";
public static void up(int i, String s){
//Debug
System.out.println("Before Change");
System.out.println("I: " + i);
System.out.println("S: " + s);
System.out.println("Num: " + num);
System.out.println("Text: " + text);
//Code
i = i + 3;
s = String.valueOf(i);
//Debug
System.out.println("After Change");
System.out.println("I: " + i);
System.out.println("S: " + s);
System.out.println("Num: " + num);
System.out.println("Text: " + text);
}
public static void main(String[] args) {
up(num, text);
//Debug
System.out.println("Out of Scope");
System.out.println("Num: " + num);
System.out.println("Text: " + text);
}
The int i and the String s you are passing to your function are passed by value. It means that you only receive a copy of the variable. An action on the variable won't affect its original value.
You can modify you method and make it return an object containing the modified values:
Create a new class to encapsulate the modified values :
class Result{
int i;
String s;
public Result(int i, String s){
this.i = i;
this.s = s;
}
}
Now your method can return this Result
public static Result up(int i, String s){
//Code
i = i + 3;
s = String.valueOf(i);
return new Result(i, s);
}
You can then have access to the modified values in your main method:
public static void main(String[] args) {
Result r = up(num, "test");
System.out.println("int result " + r.i);
System.out.println("string result " + r.s);
}
You can do it with a WrapperClass. This is because Java uses Pass By Value as others mentioned. The workaround is to create WrapperClass if there are multiple values. Here is how you can modify the class using WrapperClass. When working with Corba Java frameworks provide Holder Classes to give the reference semantics.
static int num = 2;
static String text = "3";
public static void up(WrapperClass w){
//Debug
System.out.println("Before Change");
System.out.println("I: " + w.i);
System.out.println("S: " + w.s);
System.out.println("Num: " + num);
System.out.println("Text: " + text);
//Code
w.i = w.i + 3;
w.s = String.valueOf(w.i);
//Debug
System.out.println("After Change");
System.out.println("I: " + w.i);
System.out.println("S: " + w.s);
System.out.println("Num: " + num);
System.out.println("Text: " + text);
}
public static void main(String[] args) {
WrapperClass w = new WrapperClass();
w.i = num;
w.s = text;
up(w);
//Debug
System.out.println("Out of Scope");
System.out.println("Num: " + w.i);
System.out.println("Text: " + w.s);
}
static class WrapperClass {
public int i;
public String s;
}
You just pass the copy-value of the variables i when call method up(int i, String s).It will not change the value in method up.
Read this post:Is Java “pass-by-reference” or “pass-by-value”?
There are 2 answers. The first answer is for 4 consecutive numbers and the answer is provided: 5832. The 2nd answer is for 13 consecutive numbers and it's the answer they want me to input. This answer is: 23514624000.
My answer for the 1st questions is: 29760696 which is impossible and way off
public class test {
public static void main(String[] args)
{
String big = "73167176531330624919225119674426574742355349194934"
+ "96983520312774506326239578318016984801869478851843"
+ "85861560789112949495459501737958331952853208805511"
+ "12540698747158523863050715693290963295227443043557"
+ "66896648950445244523161731856403098711121722383113"
+ "62229893423380308135336276614282806444486645238749"
+ "30358907296290491560440772390713810515859307960866"
+ "70172427121883998797908792274921901699720888093776"
+ "65727333001053367881220235421809751254540594752243"
+ "52584907711670556013604839586446706324415722155397"
+ "53697817977846174064955149290862569321978468622482"
+ "83972241375657056057490261407972968652414535100474"
+ "82166370484403199890008895243450658541227588666881"
+ "16427171479924442928230863465674813919123162824586"
+ "17866458359124566529476545682848912883142607690042"
+ "24219022671055626321111109370544217506941658960408"
+ "07198403850962455444362981230987879927244284909188"
+ "84580156166097919133875499200524063689912560717606"
+ "05886116467109405077541002256983155200055935729725"
+ "71636269561882670428252483600823257530420752963450"
;
//System.out.println(big.length());
int product=1;
int newProduct=0;
for(int i=0;i<big.length()-1-4;i++)
{
product=1;
for(int j=i+1;j<i+5;j++)
{
product=(big.charAt(i)-48)*(big.charAt(j)-48)*product;
}
if(product>newProduct)
{
newProduct=product;
}
}
System.out.println(newProduct);
}
My answer for the 2nd question is: 2135048192 and is a lot closer. Why is this the case? My code is as follows. thanks
public class test {
public static void main(String[] args)
{
String big = "73167176531330624919225119674426574742355349194934"
+ "96983520312774506326239578318016984801869478851843"
+ "85861560789112949495459501737958331952853208805511"
+ "12540698747158523863050715693290963295227443043557"
+ "66896648950445244523161731856403098711121722383113"
+ "62229893423380308135336276614282806444486645238749"
+ "30358907296290491560440772390713810515859307960866"
+ "70172427121883998797908792274921901699720888093776"
+ "65727333001053367881220235421809751254540594752243"
+ "52584907711670556013604839586446706324415722155397"
+ "53697817977846174064955149290862569321978468622482"
+ "83972241375657056057490261407972968652414535100474"
+ "82166370484403199890008895243450658541227588666881"
+ "16427171479924442928230863465674813919123162824586"
+ "17866458359124566529476545682848912883142607690042"
+ "24219022671055626321111109370544217506941658960408"
+ "07198403850962455444362981230987879927244284909188"
+ "84580156166097919133875499200524063689912560717606"
+ "05886116467109405077541002256983155200055935729725"
+ "71636269561882670428252483600823257530420752963450"
;
//System.out.println(big.length());
int product=1;
int newProduct=0;
for(int i=0;i<big.length()-1-13;i++)
{
product=1;
for(int j=i+1;j<i+14;j++)
{
product=(big.charAt(i)-48)*(big.charAt(j)-48)*product;
}
if(product>newProduct)
{
newProduct=product;
}
}
System.out.println(newProduct);
}
You are experiencing integer overflow (use a long). Character.digit(char,int) (where the second argument is the radix) can get you the int value of a char). Also, you can use Math.max(long, long) to get the maximum of two long(s). Putting that together, it might look something like
long max = 0;
for (int i = 0; i < big.length() - 13; i++) {
String str = big.substring(i, i + 13);
long product = 1;
for (char ch : str.toCharArray()) {
product *= Character.digit(ch, 10);
}
max = Math.max(max, product);
}
System.out.println(max);
And I get (the expected)
23514624000
So for an assignment I have to create an application whose main() method holds two variables. After declaring the variables and assigning an integer to each of them, I have to run both through the same 3 methods. I was thinking that I have to create a class for the variables, but honestly have no idea where to begin. So far, I have figured out how to run one of the integers through the methods, but I can't get both to pass through the same methods.
Here is my work so far:
public class ArithmeticMethods{
public class integer
{
int firstInteger = 10;
int secondInteger = 20;
}
public static void main(String[] args) {
displayNumberPlus10();
displayNumberPlus100();
displayNumberPlus1000();
System.out.println(firstInteger + " +" + " 10" + " is " + displayNumberPlus10());
System.out.println(firstInteger + " +" + " 100" + " is " + displayNumberPlus100());
System.out.println(firstInteger + " +" + " 1000" + " is " + displayNumberPlus1000());
}
public static int displayNumberPlus10() {
int numberPlus10;
numberPlus10 = (firstInteger + 10);
return numberPlus10;
}
public static int displayNumberPlus100() {
int numberPlus100;
numberPlus100 = (firstInteger + 100);
return numberPlus100;
}
public static int displayNumberPlus1000() {
int numberPlus1000;
numberPlus1000 = (firstInteger + 1000);
return numberPlus1000;
}
}
Right now the methods are set to only run the first variable and with my ATTEMPT at creating a class, the program doesn't work at all. Any advice would be greatly appreciated.
Also I apologize if the code looks ugly. I am very new to this.
You need to add parameters to your methods. The result should look something like this:
public static int displayNumberPlus10(int input) {
return (input + 10);
}
...
And can be called like this:
int first = 10;
int second = 20;
displayNumberPlus10(first);
displayNumberPlus10(second);