"Multiple markers at this line" error at constructor - java

So I am using gpdraw as a library to draw stuff for my computer science class, and I'm trying to run this in Eclipse and I put the main method but I'm still getting errors.
import gpdraw.*;
public class House {
public static void main(String[] args) {
private DrawingTool myPencil;
private SketchPad myPaper;
public House() {
myPaper = new SketchPad(500, 500);
myPencil = new DrawingTool(myPaper);
}
public void draw() {
myPencil.up();
myPencil.turnRight(90);
myPencil.forward(20);
myPencil.turnLeft(90);
myPencil.forward(20);
myPencil.turnRight(20);
myPencil.forward(200);
}
}
}

You're trying to stuff everything into the main method. That won't work. Instead, have main call draw (on an instance of the class, a context which a static method does not have available) and define everything in the class, not a method.
import gpdraw.*;
public class House {
public static void main(String[] args) {
House instance = new House();
instance.draw();
}
private DrawingTool myPencil;
private SketchPad myPaper;
public House() {
myPaper = new SketchPad(500, 500);
myPencil = new DrawingTool(myPaper);
}
public void draw() {
// stuff
}
}

Java does not allow nesting methods and/or constructors.
You need something like this:
import gpdraw.*;
public class House {
private DrawingTool myPencil;
private SketchPad myPaper;
public House() {
myPaper = new SketchPad(500, 500);
myPencil = new DrawingTool(myPaper);
}
public void draw() {
myPencil.up();
myPencil.turnRight(90);
myPencil.forward(20);
myPencil.turnLeft(90);
myPencil.forward(20);
myPencil.turnRight(20);
myPencil.forward(200);
}
public static void main(String[] args) {
// whatever
}
}

Related

is it safe to pass object of same class, as parameter, to a method of same class in java

I am starting to learn Java and was wondering if I it's normal/safe to pass an object of same class as a parameter to a method in the same class? I tried it and it works, just wanted to know if it's a standard practice.
code snippet below;
class App{
private int databaseFlag=0;
private String s;
public App() {
// TODO Auto-generated constructor stub
s = "value";
}
public static void main(String[] args) {
App app = new App();
app.init(app);
app.methodOne();
app.methodTwo();
}
public void init(App app){
DatabaseClass dbc = new DatabaseClass();
app.databaseFlag = dbc.callDatabaseMethodAndGetFlag(s);
}
public void methodOne(){
// do something with databaseFlag
}
public void methodTwo(){
// do something more then reset databaseFlag=0;
}
}
Even though, with in object's method, you can call other method by its name i.e.,
class App {
private void init () {
//some code
someOtherMethod();
}
private void someOtherMethod() {
//someother code
}
public static void main(String[] args) {
App a = new App();
a.init()
}
}
See how someOtherMethod is being called from init? You don't need your class reference here.
To answer your specific question, In Java, every object has access to its reference and that is called this, you don't need to pass in the app object. if you absolutely have to refer the object variables, you can do it by this
class App{
private int databaseFlag=0;
private String s;
public App() {
// TODO Auto-generated constructor stub
s = "value";
}
public static void main(String[] args) {
App app = new App();
app.init();
app.methodOne();
app.methodTwo();
}
public void init(){
DatabaseClass dbc = new DatabaseClass();
this.databaseFlag = dbc.callDatabaseMethodAndGetFlag(s);
}
public void methodOne(){
// do something with databaseFlag
}
public void methodTwo(){
// do something more then reset databaseFlag=0;
}
}

perform methods from other class when button is clicked in jFrame

I have a class named Parser which gets some input and do some calculations and output the results. I also have a jFrame, which has some text fields. I am misunderstanding how to run the parser and use the inputs from the jFrame. I don't know if I should implement the action Listener in my Parser class? or should I import all my Parser class methods in the jFrame? should I have run method in my main of the Parser or should I use the void run in the jframe class??
Here is my class Parser:
public class Parser{
public static List getXKeywords(String Url, int X, String html) throws Exception {
//somemethod with someoutput
}
public static void main(String[] args) throws Exception {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
SpyBiteDemo Sp = new SpyBiteDemo();
Sp.setVisible(true);
int X=Sp.getKeywordcount();
//this top line is not correct because it can only be done when the jframe jButton1 was clicked
}
});
}
}
and here is the jFrame;
public class SpyBiteDemo extends javax.swing.JFrame {
/**
* Creates new form SpyBiteDemo
*/
public SpyBiteDemo() {
initComponents();
}
public String getKeywordcount()
{
return jTextField4.getText();
}
//some methods
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//get the input from the jframe
//feed it to the parser?? how???
String SeedUrl=jTextField1.getText();
Parser P=new Parser();
//I don't have access to methods
because they are static
}
}
here I am trying to get keywordcount variable from the jFrame which is the int X in the getXKeywords method.
I solved my problem with the help of this link
I created a constructor in my parser class and also included a jframe in the parser class as follow:
public class Parser {
SpyBiteDemo Sp=new SpyBiteDemo();
public Parser(SpyBiteDemo Sp)
{
this.Sp=Sp;
int X = Sp.getXKeywords();
//do whatever
}
and in the action performed of the jframe class I call my parser constructor class:
public class SpyBiteDemo extends javax.swing.JFrame {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Parser P=new Parser(this);
}
}

How is it possible to communicate between two classes in Java using an interface?

Hi ive been reading on some similar topics here but none of them answer my question. Some say you cant even do this which is not a good thing since I cant finnish my course in that case.
Heres som simple code. Think of each block as a separate class.
public interface Interface {
void printMessage(String meddelande);
}
public class Model implements Interface {
String message = "hej!";
public static void main(String[] args) {
Model model1 = new Model();
View view1 = new View();
model1.printMessage(model1.message); //Ska jag anropa funktionen såhär ens?
}
public void printMessage(String str) {
}
}
public class View implements Interface {
printMessage(String str) {
}
}
So, how is it now possible to tel the view to print this string from the model class without the classes knowing about each other? Its not allowed to send a reference of the model-objekt to the view-object. ; (
Define an Interface:
public interface MyInterface {
void printMessage(String str);
}
Define a class that can trigger the notification:
public class ClassNotifier {
MyInterface mInterface;
public ClassNotifier(MyInterface mInterface) {
this.mInterface = mInterface;
}
public void triggerTheMsg(String msg) {
if (mInterface != null) {
mInterface.printMessage(msg);
}
}
}
Define a class that will be informed:
public class InformedClass implements MyInterface {
public static void main(String[] args) throws Exception {
InformedClass c = new InformedClass();
ClassNotifier cn = new ClassNotifier(c);
}
#Override
public void printMessage(String newMsg) {
System.out.println("A new msg is here: " + newMsg);
}
}
How does it works?:
this is named a callback parttern, the class ClassNotifier has a reference to the interface MyInterface, which is impl. by Informed class to, so every time the ClassNotifier calls the method printMessage, the method printMessage in the class Informed will be triggered too.
I advice you to use dependency injection, for example:
public class Model {
String message = "hej!";
Interface printer;
public void Model(Interface printer) {
printer = printer;
}
public static void main(String[] args) {
Model model1 = new Model(new View());
model1.printMessage(model1.message);
}
public void printMessage(String str) {
printer.printMessage(str);
}
}

Truncate the method in java

I have the following code:
public class Search {
private Desktop desktop = new Desktop();
#Before
public void baseState() {
BrowserBaseState baseState = new BrowserBaseState("silk4j.settings");
baseState.execute(desktop);
}
#Test
public void searchNames() {
desktop.<BrowserApplication>find("//BrowserApplication").<BrowserWindow>find("//BrowserWindow").<DomButton>find("//INPUT[#id='edit-submit']").select();
}
}
I was able to truncate the Test method to this:
public class Search {
private Desktop desktop = new Desktop();
BrowserApplication app;
#Before
public void baseState() {
BrowserBaseState baseState = new BrowserBaseState("silk4j.settings");
app = baseState.execute(desktop);
}
#Test
public void searchNames() {
app.<BrowserWindow>find("//BrowserWindow").<DomButton>find("//INPUT[#id='edit-submit']").select();
}
How do I truncate the method even further? I would like to be able to use something like this:
win.<DomButton>find("//INPUT[#id='edit-submit']").select();
instead of this chunky long:
desktop.<BrowserApplication>find("//BrowserApplication").<BrowserWindow>find("//BrowserWindow").<DomButton>find("//INPUT[#id='edit-submit']").select();
Please paste the whole class in your response?
public class Search {
private Desktop desktop = new Desktop();
BrowserWindow win;
#Before
public void baseState() {
BrowserBaseState baseState = new BrowserBaseState("silk4j.settings");
win = baseState.execute(desktop).find("//BrowserWindow");
}
#Test
public void searchNames() {
win.<DomButton>find("//INPUT[#id='edit-submit']").select();
}
}

Simple App Won't Compile in Eclipse (with plugin)?

my code, being practically identical to the code given in BlackBerry's tutorial, has a syntax error in Eclipse. i'm sure there is some small but i'm just not seeing, but my coworker could not find it as well. any ideas would be greatly appreciated. thanks!
Code:
pushScreen(new ABCScreen());
Error:
Cannot make a static reference to the
non-static method pushScreen(Screen)
from the type UiApplication
here is the complete source:
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class AwesomeBBCalculator extends UiApplication {
public AwesomeBBCalculator() {
AwesomeBBCalculator app = new AwesomeBBCalculator();
app.enterEventDispatcher();
}
public static void main(String[] args) {
pushScreen(new ABCScreen()); // ERROR LINE
}
}
final class ABCScreen extends MainScreen {
public ABCScreen() {
super();
// add title
LabelField title = new LabelField("Awesome BlackBerry Calculator",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
}
public boolean onClose() {
Dialog.alert("Thanks for using the Awesome BlackBerry Calculator!\nGoodbye.");
System.exit(0);
return true;
}
}
The pushScreen method can only be called within an instance of UiApplication. You are trying to call it from a static main method. That does not work. Do this instead...
public void foo()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new ABCScreen()).foo();
}
public void class1()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new NewScreen()).class1();
}
try making an object for the ABCScreen class and then use it or u may try this also:
UiApplication.getUiApplication().pushScreen(new ABCScreen());

Categories