Like the in-built Math class, there are a couple of methods that one can use without importing the Math class. e.g
int io = (int) Math.random();
and notice the import region: no MATH whatsoever
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
but seeing, Math set doesn't have everything i needed, i created mine in a new class, but i can't seem to figure out what to do so i can be able to use it.
Taking a hint from the Math.java file, I've made my class final and my methods static but no avail..
Here's an excerpt of my code
package customops.Sets;
/**
*
* #author Kbluue
*/
public final class SetOpz {
public SetOpz(){}
public static int setMax(int[] set){
int out = set[0];
for(int i=1; i<set.length; i++){
out = Math.max(out, set[i]);
}
return out;
}
how do i use just the import command without having to copy and paste the SetOpz class in the DTL package?
You don't need to import Math explicitly because it is included by default. To use your own code you will have to import it. If you're using IntelliJ or Eclipse or some other smart IDE it will offer to import it for you automatically. Otherwise add a import statement at the top:
import customops.Sets.SetOpz;
You can import your method wherever you want to use with the following import statement
import static customops.Sets.SetOpz.setMax;
Related
I am creating a hook using AndHook to test some functions getting called. I need to show a Toast inside a method without being able to get the context object (I can't directly import MainActivity because I am injecting the script without having the corresponding package when compiling so I can't use MainActivity.this). Here's the code sample:
import andhook.lib.HookHelper;
import android.widget.Toast;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
public class AndHookConfig {
#HookHelper.Hook(clazz = BitmapFactory.class)
private static Bitmap decodeFile(BitmapFactory thiz, String path) {
Toast.makeText(Class.forName("com.example.activity.MainActivity").this, "Bitmap.decodeFile("+path+")", Toast.LENGTH_LONG).show();
return (Bitmap)(HookHelper.invokeObjectOrigin(thiz, path));
}
}
I think the only way to do this is using reflection but the code sample doesn't compile and results in an error. Do you have any solutions ?
Thanks in advance.
In Java I was able to run my code as:
(This are just sample naming)
import com.projectname.api.APIOne;
import com.projectname.api.APITwo;
import com.projectname.api.APIThree;
import com.projectname.api.APIFour;
import com.projectname.api.MainAPI;
public class TestMain {
public static void main(String[] args) {
APIOne a = APIOne.getName();
APITwo b = APIThree.getAddress();
APIFour d = b.getEmail();
MainAPI mainapi = new MainAPI();
mainapi.setEmail(d)
}
}
It is running okay, I tried converting this to Python as:
import com.projectname.api.APIOne as APIOne;
import com.projectname.api.APITwo as APITwo;
import com.projectname.api.APIThree as APIThree;
import com.projectname.api.APIFour as APIFour;
def test():
a = APIOne.getName();
b = APIThree.getAddress();
d = b.getEmail();
mainapi = MainAPI();
mainapi.setEmail(d)
test()
But is this the right way of instantiating? It make me confuse on instantiating.
Hope you could help me.
Importing a class from a java package or python module is normally written as:
from java.lang import Math
Rather than:
import java.lang.Math as Math
But, your code is correct.
I don't understand why you are confused, but this is correct, you could check the Jython documentation about instantiating Java objects using Jython and instantiates the objects the same way as you do.
I have the following question: I am trying to execute the usConstitution wordcram example (code follows) and if provided as is the code executes in eclipse, the applet starts and the word cloud is created. (code follows)
import processing.core.*;
//import processing.xml.*;
import wordcram.*;
import wordcram.text.*;
import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class usConstitution extends PApplet {
/*
US Constitution text from http://www.usconstitution.net/const.txt
Liberation Serif font from RedHat: https://www.redhat.com/promo/fonts/
*/
WordCram wordCram;
public void setup() {
size(800, 600);
background(255);
colorMode(HSB);
initWordCram();
}
public void initWordCram() {
wordCram = new WordCram(this)
.fromTextFile("http://www.usconstitution.net/const.txt")
.withFont(createFont("https://www.redhat.com/promo/fonts/", 1))
.sizedByWeight(10, 90)
.withColors(color(0, 250, 200), color(30), color(170, 230, 200));
}
public void draw() {
if (wordCram.hasMore()) {
wordCram.drawNext();
}
}
public void mouseClicked() {
background(255);
initWordCram();
}
static public void main(String args[]) {
PApplet.main(new String[] { "--bgcolor=#ECE9D8", "usConstitution" });
}
}
My problem is the following:
I want to pass through main (which is the only static class) an argument so as to call the usConstitution.class from another class providing whichever valid filename I want in order to produce its word cloud. So how do I do that? I tried calling usConstitution.main providing some args but when I try to simply print the string I just passed to main (just to check if it is passed) I get nothing on the screen. So the question is How can I pass an argument to this code to customize .fromTextFile inside initWordCram ?
Thank you a lot!
from: https://wordcram.wordpress.com/2010/09/09/get-acquainted-with-wordcram/ :
Daniel Bernier says:
June 11, 2013 at 1:13 am
You can’t pass command-line args directly to WordCram, because it has no executable.
But you can make an executable wrapper (base it on the IDE examples that come with WordCram), and it can read command-line args & pass them to WordCram as needed.
FYI, it’ll still pop up an Applet somewhere – AFAIK, you can’t really run Processing “headless.” But that’s usually only a concern if you’re trying to run on a server.
I have a general question about java. Because I want to create StronaGlowna.java (class) where I have place all buttons, check box and other GUI component which I want to display in main class. The first question is this right way, it's correct ? or maybe is better way to do this thing. My code look this:
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class Main extends JFrame {
private static final long serialVersionUID = -4575271483481196192L;
Container pane;
CardLayout layout;
public Main() throws FileNotFoundException, IOException {
layout = new CardLayout();
setLayout(layout);
pane = this.getContentPane();
/*Page: Strona główna */
JPanel newPanel = new JPanel();
pane.add("New", newPanel);
JButton przycisk = new JButton("Przycisk");
newPanel.add(przycisk);
...
In "pane.add("New", newPanel);" I want to display elements from:
package aplikacja.glowna;
import javax.swing.JButton;
import javax.swing.JPanel;
public class StronaGlowna {
public void StronaGlownaDisplay() {
JPanel newPanel = new JPanel();
JButton przycisk2 = new JButton("Przycisk");
newPanel.add(przycisk2);
}
}
Can I import/display all class StronaGlowna in main() something like a include in PHP ? What do You thing about my idea, it's correct or I'm wrong ? Thanks for help and discussion.
It sounds like the way Netbeans handling GUI. You may view the article in http://netbeans.org/kb/docs/java/quickstart-gui.html, it may help you understand how the GUI works since Netbeans can generate code for you. You can always import class and create object to access methods (often public methods) . I think it is not like a include in PHP. PHP include is like to directly include the source code, but jave is not.
First - Never, never, never, code in Main class. Call a method from it and then start your staff in another class. And, of course, don't extend it. And the constructor is neither a good idea. All of these are bad practices. Now, going into your problem, my suggestion is that you make StronaGlowna extend JPanel, and then obtain an instance of it through a public constructor, and use that instance as the parameter for the constructor of JScrollPane. That will make the scrollPane act as a 'screen' inside which you can see the contents of StronaGlowna, which is what I understand you're after.
I'm trying to use the wordalignment in the BerkeleyAligner.jar file from http://code.google.com/p/berkeleyaligner/ in my own java class.
I have already added the .jar file into my buildpath.
What parameters does the edu.berkeley.nlp.wordAlignment.combine.CombinedAligner take?
What does the edu.berkeley.nlp.wordAlignment.combine.CombinedAligneroutput?
What i have are 2 input files that are already sentence aligned; i.e. the sentence from line number X from the sourceFile is the same (but in a different language) as the sentence from line number X of the targetFile.
import edu.berkeley.*;
import edu.berkeley.nlp.wa.mt.Alignment;
import edu.berkeley.nlp.wa.mt.SentencePair;
public class TestAlign {
BufferedReader brSrc = new BufferedReader(new FileReader ("sourceFile"));
BufferedReader brTrg = new BufferedReader(new FileReader ("targetFile"));
String currentSrcLine;
while ((currentSrcLine = brSrc.readLine()) !=null) {
String currentTrgLine = brTrg.readline();
// Reads into BerkeleyAligner SentencePair format.
SentencePair src2trg = new SentencePair(sentCounter, params.get("source"),
Arrays.asList(srcLine.split(" ")), Arrays.asList(trgLine.split(" ")));
// How do i call the BerkeleyAligner??
// -What parameters does the CombinedAligner takes?
// -What does the function/class returns?
// I assume it returns a list of strings.
// Is there a class in BerkeleyAligner to read the output?
// Please provide some example, thank you!!
Alignment output = edu.berkeley.nlp.wordAlignment.combine.CombinedAligner
.something.something(currentSrcLine, currentTrgLine);
}
}
e.g. sourceFile:
this is the first line in the textfile.
that is the second line.
foo bar likes to eat bar foo.
e.g. targetFile:
Dies ist die erste Textzeile in der Datei.
das ist die zweite Zeile.
foo bar gerne bar foo essen.
Actual Answer
You just wanted to align text (from a target file and a source file), right?
If so, after creating a sentence pair, you did not even need to put them in a CombinedAligner.
You could get an Alignment: (SentencePair, boolean) from that. The boolean is if you want a tree alignment.
Putting it into the constructor will generate an Alignment automatically!
So simple!
This is where I got the code: http://code.google.com/p/berkeleyaligner/source/browse/trunk/src/edu/berkeley/nlp/wa/mt/Alignment.java
UPDATE
Unfortunately, I misunderstood your question, and posted an irrelevant response.
However, I downloaded the jar file, found CombinedAligner.class, and decompiled it.
Here's what I got:
package edu.berkeley.nlp.wordAlignment.combine;
import edu.berkeley.nlp.mt.Alignment;
import edu.berkeley.nlp.mt.SentencePair;
import edu.berkeley.nlp.wordAlignment.PosteriorAligner;
import edu.berkeley.nlp.wordAlignment.WordAligner;
import fig.basic.Fmt;
import fig.basic.ListUtils;
import java.util.ArrayList;
import java.util.List;
public abstract class CombinedAligner extends PosteriorAligner {
private static final long serialVersionUID = 1;
WordAligner wa1;
WordAligner wa2;
public CombinedAligner (WordAligner, WordAligner)
public String getName()
public Alignment alignSentencePair(SentencePair)
public List alignSentencePairReturnAll(SentencePair)
public void setThreshold(int)
abstract Alignment combineAlignments(Alignment, Alignment, SentencePair)
}
It seems that the Alignment class you're using is edu.berkeley.nlp.mt.Alignment.
Anyway, CombinedAligner is abstract, so you can't instantiate it. And I don't know what the .something's are, because there is no static method or field.
I think that what you want, however, is alignSentencePair(SentencePair).
To get this, you need to use a subclass of CombinedAligner, as CombinedAligner is abstract.
So, after poking around the files, I found these subclasses:
edu.berkeley.nlp.wordAlignment.combine.HardUnion
edu.berkeley.nlp.wordAlignment.combine.HardIntersect
edu.berkeley.nlp.wordAlignment.combine.SoftUnion
edu.berkeley.nlp.wordAlignment.combine.SoftIntersect
You can use these instead of CombinedAligner and insert your two sentences as a SentencePair!
After checking, I realized that WordAligner is also abstract!
package edu.berkeley.nlp.wordAlignment;
import edu.berkeley.nlp.mt.Alignment;
import edu.berkeley.nlp.mt.SentencePair;
import fig.basic.LogInfo;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public abstract class WordAligner implements Serializable {
private static final long serialVersionUID = 1;
protected String modelPrefix;
public WordAligner ()
public abstract String getName()
public void setThreshold(double)
public Alignment alignSentencePair(SentencePair)
public Map alignSentencePairs(List)
public Alignment thresholdAlignment(Alignment, double)
public String getModelPrefix()
public String toString()
}
I found a subclass, though:
edu.berkeley.nlp.wordAlignment.IterWordAligner
Unfortunately, this is still abstract.
But there's a subclass of IterWordAligner that isn't:
edu.berkeley.nlp.wordAlignment.EMWordAligner
However, the constructor is really weird.
public EMWordAligner (SentencePairState$Factory, Evaluator, boolean)
It uses an INNER CLASS in the CONSTRUCTOR!? That's terrible programming practice.
WAIT...
I found a word aligner!
http://code.google.com/p/tdx-nlp/source/browse/trunk/pa2/java/src/cs224n/assignments/WordAlignmentTester.java?r=67
Maybe that helps and you can resolve your problem with it.