Compiling errors on homework - java

I'm writing a program for my class and i'm getting compiling errors and I'm not sure why. I think it has something to do with my Test method.
public class Test1 {
public static void main(String[] args) {
String test;
public void Test(String s){
text = s;
}
Test test = new Test("ABC");
System.out.println(test);
}
}

public class Test1 {
String text;
public Test1(){
}
public Test1(String s){
this.text = s;
}
public static void main(String[] args) {
Test1 test = new Test1("ABC");
System.out.println(test.text);
}
}
Try this..

Related

Why eclipse show me error in the line where i have written public static void main(String[] args)

So why public static void main(String[] args) I got an error. what can i do to resolve it?
package linkedList;
public class HackerRank {
public class Solution {
// Complete the aVeryBigSum function below.
public long aVeryBigSum(long[] ar) {
long a=0;
for(int i=0;i<ar.length;i++){
a=ar[i]+a;
}
return a;
}
public static void main(String[] args) { ///why this line is not correct
Solution s= new Solution();
long[] ar= {10000,20000,30000};
System.out.println(s.aVeryBigSum(ar));
}
}
}
There is another possible solution, taking the nested Solution class out of the HackerRank class, as I see you are not doing anything with it at the moment.
public class Solution {
// Complete the aVeryBigSum function below.
public long aVeryBigSum(long[] ar) {
long a = 0;
for (int i = 0; i < ar.length; i++) {
a = ar[i] + a;
}
return a;
}
public static void main(String[] args) {
Solution s = new Solution();
long[] ar = { 10000, 20000, 30000 };
System.out.println(s.aVeryBigSum(ar));
}
}
This makes sure that your static main method works.
You can't access a static method in a non-static class. There are 2 possible solutions for this problem:
- 1. Make Solution static
public static class Solution {
public static void main(String[] args) {
//...
}
}
- 2. Remove the static from the main method
public class Solution {
public void main(String[] args) {
//...
}
}

Override toString() method java

I have this in my Main class in java
public class Main {
#Override
public String toString(){
return "lol";
}
public static void main(String[] args) {
int aaa=0;
System.out.println(aaa);
}
}
I Want to Override that toString() Method that implicity called.
why output is 0 not "lol" ?
why output is 0 not "lol" ?
because you are printing an integer and not an instance of that Main class
you can do the following
public class Main {
#Override
public String toString(){
return "lol";
}
public static void main(String[] args) {
// int aaa=0;
Main myMain = new Main();
System.out.println(myMain);
}
}
note that you can do
System.out.println(myMain);
the same as
System.out.println(myMain.toString());

How to find how many methods and their names used my variable in a java program?

Here is example to find how many variables used in my class. But I need to find how many methods are using my variable with in a class.
Test class having four methods but sample1 variable is used in test1() and test3() methods. I want output as test1(),test2() are used sample1 variable
import java.lang.reflect.Field;
public class Test {
private int sample1;
private int sample2;
private int sample3;
public void test1()
{
System.out.println(sample1);
}
public void test2()
{
System.out.println(sample2);
}
public void test3()
{
System.out.println(sample1);
}
public void test4()
{
System.out.println(sample3);
}
public static void main(String[] args) {
Test t = new Test();
Field f[] =Test.class.getDeclaredFields();
for (int i = 0; i < f.length; i++)
{
System.out.println("Variable Name is : " + f[i].getName());
}
}
}
Can this help?
import java.lang.reflect.Field;
import java.util.*;
public class Main {
private int sample1;
private int sample2;
private int sample3;
private ArrayList<String> whoUseSameple1= new ArrayList<String>();
int getSample1(String methodname){
whoUseSameple1.add(methodname);
return sample1;
}
public void test1(){
System.out.println(getSample1(new Object(){}.getClass().getEnclosingMethod().getName()));
}
public void test2(){
System.out.println(sample2);
}
public void test3(){
System.out.println(getSample1(new Object(){}.getClass().getEnclosingMethod().getName()));
}
public void test4(){
System.out.println(sample3);
}
public static void main(String[] args) {
Main t = new Main();
t.test1();
t.test2();
t.test3();
t.test4();
for (String s:t.whoUseSameple1){
System.out.print(s+" ");
}
System.out.print("are usd Sample1 Variable\n");
// Field f[] =Main.class.getDeclaredFields();
// for (int i = 0; i < f.length; i++)
// {
// System.out.println("Variable Name is : " + f[i].getName());
// }
}
}

How can I pass a pointer to some object as argument to a method?

For example, I have such code:
SomeClass item = new SomeClass();
OtherClass.someVoidMethod(item); //there was some changes with item
and then in class, where was called method someVoidMethod(item) item will not change, cause we use in this method copy of that item. In C/C++ there is pointers. I'm looking for something like that in Java.
there is better example:
There i cant edit string from method
public class Main {
public static void main(String[] args) {
String string = "String";
changeIt(string);
System.out.println(string);
}
public static void changeIt(String string){
string = string + " edited";
}
And what suntax I need, to do that? I tied to use *string and so on, but i do this wrong.
i find that this code wil not work the way i want
public class Main {
public static void main(String[] args) {
SomeClass someClass = new SomeClass();
OtherClass.changeIt(someClass.getValue());
}
}
class SomeClass {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
class OtherClass {
public static void changeIt(String string){
string = "someStr";
}
}
so i need to use something like that:
public static void main(String[] args) {
SomeClass someClass = new SomeClass();
someClass.setValue(OtherClass.changeIt(someClass.getValue()));
}
public static String changeIt(String string){
return "someStr";
}
and there is no other way?
To make a Long Story short: use Parameters as Inputs to a method, and use the return value as Output. This will make your code better readable and easier to maintain.
For example:
public class Main {
public static void main(String[] args) {
String string = "String";
String modifiedString = changeIt(string);
System.out.println(modifiedString);
}
public static String changeIt(String string) {
return string + " edited";
}
}
If you wrap your string into a mutable Container, than you can modify it in your method. However for your example code I would strongly recommend to not use this Approach!
class StringContainer {
String content;
}
public class Main {
public static void main(String[] args) {
StringContainer container = new StringContainer();
container.content = "String";
changeIt(container);
System.out.println(container.content);
}
public static void changeIt(StringContainer container) {
container.content += " edited";
}
}
The StringBuilder, as mentioned by #aioobe, would also be a good alternative to use.

#MockClass is not working

I am new to jmockit and trying to execute the following online example.
The #MockClass is not working. My BookStore's getBookTitle() method is calling the function of orginal class instead of the mock class.
BookStore class:
public class BookStore {
public String getBookTitle(String isbn){
return BookStoreService.getBookTitle(isbn);
}
}
BookStoreService class:
public class BookStoreService {
public static String getBookTitle(String isbn){
return "Random";
}
}
Test class:
public class BookStoreTest {
private static Map<String, String> bookMap = new HashMap<String, String>(2);
#BeforeClass
public static void setup() {
System.out.println("in setup()");
bookMap.put("0553293354", "Foundation");
bookMap.put("0836220625", "The Far Side Gallery");
}
#MockClass(realClass = BookStoreService.class)
public static class MockBookstoreService {
#Mock
public static String getBookTitle(String isbn) {
System.out.println("in getBookTitle()");
if (bookMap.containsKey(isbn)) {
return bookMap.get(isbn);
} else {
return null;
}
}
}
#Test
public void testGetBookTitle() throws Exception {
System.out.println("in testGetBookTitle()");
final String isbn = "0553293354";
final String expectedTitle = "Foundation";
BookStore store = new BookStore();
String title = store.getBookTitle(isbn);
System.out.println(title); // This prints "Random" instead of "Foundation"
Assert.assertEquals(title, expectedTitle);
}
}
PS: I am using TestNG
Using the latest stable version of jmockit you could do it like this:
#BeforeClass
public static void setup() {
System.out.println("in setup()");
bookMap.put("0553293354", "Foundation");
bookMap.put("0836220625", "The Far Side Gallery");
new MockUp<BookStoreService>() {
#Mock
public String getBookTitle(String isbn) {
System.out.println("in getBookTitle()");
if (bookMap.containsKey(isbn)) {
return bookMap.get(isbn);
} else {
return null;
}
}
};
}
Remove the obsolete block:
public static class MockBookstoreService{...}

Categories