Eclipse Java: Eclipse complains of missing curly brace in class - java

the following code in eclipse gives a "}" missing in classbody when in eclipse, but compiles perfectly well from the terminal. Any clues?
package quiz;
public class Session {
static int currentQuestion = 0;
private Sentence[] sentences; // for building questions
private Question[] questions;
public void generateReport(Session publishSession) {
}
public int getRightQuestionCount() {
}
public int getWrongQuestionCount() {
}
public int calculatePercent() {
}
public Question getQuestionAtIdx(int index) {
return questions[index];
}
public Question getPreviousQuestion() {
return getQuestionAtIdx(--currentQuestion);
}
public Question getNextQuestion() {
return getQuestionAtIdx(--currentQuestion);
}
public void setQuestionAtIdx(int index, Question) {
}
}

Toward the end, you're declaring a function with two parameters; for the second parameter you specified the type, but no name. Maybe that's it.
public void setQuestionAtIdx(int index, Question <<missing name>>) {
}

that last method
public void setQuestionAtIdx(int index, Question/*has a missing arguement but only its type*/) {
}
so add
public void setQuestionAtIdx(int index, Question question) {
}

I think the problem is here:
public void setQuestionAtIdx(int index, Question) { }
Question has no identifier.

Eclipse sometimes gets confused. If the code is syntactically correct, try restarting eclipse.
your code as you pasted above will not compile due to several issues, notably missing parameter names and return values as mentioned above.

Related

Setting a boolean value in Constructor, changing it in a method, return it in another

I'm a newbie yet, so please feel free to accuse me of asking silly things xD. I just started coding. So I want to specify my question to make it clear for you. I'm stuck regarding this: We need a constructor (public DoggoII) which sets our value to false. Then we need a method (makeGoodBoi()) to set the value to true and then I need another method (isGoodBoi()) to return the value of the private field goodBoi and System.out.print some stuff later. Consider the rest of the code as done. Can someone give me a hint or something on how to do that? Because I'm kinda lost.
The question is if I have a fault that I can't find and how to return a boolean value in another method in general. Thanks!
public class Doggo {
private String name;
private boolean goodBoi;
public Doggo(String name){
this.name = name;
}
public String getName() {
return name;
}
public void makeBark() {
System.out.println(name + " said: Woof woof");
}
public Doggo (boolean goodBoi){
this.goodBoi= false;
}
public void makeGoodBoi(){
this.goodBoi = !this.goodBoi;
}
public void isGoodBoi(){
if (makeGoodBoi()){
return;
}
}
public void whosAGoodBoi() {
if (isGoodBoi()) {
System.out.println(name + " is such a good boii");
} else {
System.out.println(name + " is not a good boi :(");
}
}
public static void main(String[] args) {
Doggo dog = new Doggo("Casper");
System.out.println(dog.getName());
dog.makeBark();
}
}
Just a basic getter, use boolean as return type instead of void.
public boolean isGoodBoi() {
return goodBoi;
}
Since goodBoi is a class member and by default boolean class members are false, so you don’t have to do anything except add a getter
public boolean isGoodBoi() {
return goodBoi;
}
This will send whatever current value of class member is.
So getting this would be as simple as;
DOGGO_OBJECT.isGoodBoi();
Then we need a method (makeGoodBoi()) to set the value to true and then I need another method (isGoodBoi()) to return the value of the private field goodBoi and System.out.print some stuff later.
public void makeGoodBoi() {
this.goodBoi = true;
}

any tools to trace code using eclipse line by line?

It's been long time since I've asked a question so forgive my mistakes.
So, I have a code which I don't understand. It's dealing with interface and super/sub class. I even have the answers to it but I just don't know how it got to the answer. My question is that, how would I learn tracing or is there a way I can see which lines are executed first in Eclipse?
For example, does eclipse or any other tool allows the user to actually see which and why the lines are printing?
Here is my code. I Have the correct answer to it but I just don't know how they traced it. Any help would be appreciated.
interface Silly {
public void narf();
public void poit(Silly s);
}
public class Bird implements Silly {
public static void main(String args[]) {
System.out.println("zero");
Silly s = new SillyBird(1);
Silly s2 = new Loony();
s.poit(s2);
s2.poit(s);
System.out.println("zymurgy");
}
public Bird() {
this(0);
System.out.println("zircon");
}
public Bird(int i) {
System.out.println("zanzibar");
}
public void narf() {
System.out.println("zort");
}
public void poit(Silly s) {
s.narf();
}
}
class SillyBird extends Bird {
public SillyBird() {
System.out.println("duchess");
}
public SillyBird(int i) {
super(i);
}
public void narf() {
System.out.println("drum");
super.narf();
}
}
class Loony extends SillyBird {
public Loony() {
System.out.println("stupendous");
}
public void narf() {
System.out.println("snark");
}
}
The output of the above code was:
zero
zanzibar
zanzibar
zircon
duchess
stupendous
snark
drum
zort
zymurgy
As mentioned by #Abra in the comments you can set a breakpoint at the functions you want to have a look at and use the ‚go into‘ operation to see what is going on in the function at execution. The debugger will show you the state of all locals and globals at each step of the execution

printf error can't launch java eclipse

My printf contains an error. The rest of my code is done, but because of the error in my printf, I can't launch Java. Please help. My main class already done.
package id.web.aditya;
public class Roda {
private int diameter;
private String warna;
private String Merk;
private String Keterangan;
public String getMerk() {
return Merk;
}
public void setMerk(String merk) {
Merk = merk;
}
public int getDiameter() {
return diameter;
}
public void setDiameter(int diameter) {
this.diameter = diameter;
}
public String getWarna() {
return warna;
}
public void setWarna(String warna) {
this.warna = warna;
}
public String getKeterangan() {
return Keterangan;
}
public void setKeterangan(String keterangan) {
this.Keterangan = keterangan;
}
public void tampilanKeterangan(){
System.out.printf("Roda %s Merk: %s Warna: %s Diameter: %d \n ",
Keterangan, Merk, warna, diameter);
}
public void Berhenti(){
tampilanKeterangan();
System.out.println("Kurangi Kecepatan");
System.out.println("mulai berhenti..");
System.out.println("Akhirna berhenti");
System.out.println("--------------------");
}
public void berputar(){
tampilanKeterangan();
System.out.println("mulai berputar");
System.out.println("berputar");
System.out.println("berputar makin cepat");
System.out.println("----------------------");
}
}
package id.web.aditya;
public class Mobil {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Roda rodaUntukDitest = new Roda();
rodaUntukDitest.setDiameter(80);
rodaUntukDitest.setMerk("Achiles");
rodaUntukDitest.setWarna("Hitam");
rodaUntukDitest.setKeterangan("");
rodaUntukDitest.Berhenti();
rodaUntukDitest.berputar();
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation
problem: The method printf(Locale, String, Object[]) in the type
PrintStream is not applicable for the arguments (String, String,
String, String, int)
String format feature is introduced in java 1.5. You are apparently using a java version or a compilation option prior to that version.
If you are using eclipse open the project properties (ALT + Enter on the project). Look at the tab java compiler.
Here you can set your compiler compliance level. You probably have a newer java version installed. In this case eclipse shows you the above mention error.
I would expect to show you something like "The method printf(String, String, String, String, Integer) is undefined for the type PrintStream". but it doesn't so this might be a little bit confusing.

Signifiance of _() in Java is it some sort of constructor

I am new to Java and started learning and exploring bit about language. Could anyone explain what is significance of _() in that constructor. Is that called constructor?
public class UserRequestCache {
private final static ThreadLocal <UserRequest> t = new ThreadLocal <UserRequest>();
private static UserRequestCache instance = new UserRequestCache();
public static UserRequestCache _() {
return instance;
}
private UserRequestCache() {
}
public void checkPoint() {
if (logDebug()) {
if (getUserRequest() != null) {
logDebug(getUserRequest().toString());
}
}
}
public UserRequest getCache() {
// checkPoint();
return getUserRequest();
}
private UserRequest getUserRequest() {
return t.get();
}
public void setCache(UserRequest value) {
t.set(value);
}
}
No, it's just a very poorly named method. I recall another similar question recently, that quoted some documentation saying that even though a single underscore is a legal name, it shouldn't be used.
In this case it seems that the class is a Singleton, and the method that's usually named getInstance() has been shortened to _().
It's a funny construct that you have here. the name of the function is '_'.
So you have something like UserRequestCache._() that return a UserRequestCache.
Nothing to do with some weird Java 'magic'

Eclipse compilation error for generic method

I'm getting a compilation error in Eclipse, while using a generic method, which doesn't appear when using javac. I'm guessing it's a compiler option in Eclipse, but I can't see a relevant item in the Preferences window.
The example I'm working with is from Thinking In Java 4th Edition, the relevant code is below, copied and pasted from the downloadable examples from the book's website.
//: generics/Generators.java
// A utility to use with Generators.
import java.util.*;
import net.mindview.util.*;
public class Generators {
public static <T> Collection<T>
fill(Collection<T> coll, Generator<T> gen, int n) {
for(int i = 0; i < n; i++)
coll.add(gen.next());
return coll;
}
public static void main(String[] args) {
Collection<Coffee> coffee = fill(
new ArrayList<Coffee>(), new CoffeeGenerator(), 4);
for(Coffee c : coffee)
System.out.println(c); }
}
Generator, Coffee and CoffeeGenerator are defined elsewhere, but I don't believe they are relevant to my issue.
This code gives me the compiler error: 'The method fill(Collection, Generator, int) in the type Generators is not applicable for the arguments (ArrayList, CoffeeGenerator, int)'.
Any ideas which option I can change to make Eclipse compile the same as when I use javac from the command line?
Many thanks in advance! I did do a search (here and Google) and couldn't find an answer - apologies if this is a repeat.
Code for CoffeeGenerator:
//: generics/coffee/CoffeeGenerator.java
// Generate different types of Coffee:
import java.util.*;
import net.mindview.util.*;
public class CoffeeGenerator
implements Generator<Coffee>, Iterable<Coffee> {
private Class[] types = { Latte.class, Mocha.class,
Cappuccino.class, Americano.class, Breve.class, };
private static Random rand = new Random(47);
public CoffeeGenerator() {}
// For iteration:
private int size = 0;
public CoffeeGenerator(int sz) { size = sz; }
public Coffee next() {
try {
return (Coffee)
types[rand.nextInt(types.length)].newInstance();
// Report programmer errors at run time:
} catch(Exception e) {
throw new RuntimeException(e);
}
}
class CoffeeIterator implements Iterator<Coffee> {
int count = size;
public boolean hasNext() { return count > 0; }
public Coffee next() {
count--;
return CoffeeGenerator.this.next();
}
public void remove() { // Not implemented
throw new UnsupportedOperationException();
}
};
public Iterator<Coffee> iterator() {
return new CoffeeIterator();
}
}
Code for Generator:
//: net/mindview/util/Generator.java
// A generic interface.
package net.mindview.util;
public interface Generator<T> { T next(); } ///:~

Categories