Using a string to instantiate a class - java

Suppose I have classes Circle, Rectangle, and Triangle.
Based on input from a data file, I want to create the appropriate object. For instance, if the first line of shapes.dat is C.5 0 0, I will create a Circle object with radius 5. If the next line is R.5 3 0, I will create a Rectangle object with length 5 and width 3.
I know I could use basic if-else logic, but I was wondering whether there's a way to use the string as a means of instantiating a new object. Sort of like the exec() method in Python. Here's the code snippet describing what I want:
Scanner file = new Scanner (new File("shapes.dat"));
String s;
Map<Character,String> dict = new HashMap<Character,String>();
dict.put('C', "Circle");
dict.put('R', "Rectangle");
dict.put('T', "Triangle");
while (file.hasNextLine())
{
s = file.nextLine().trim();
String name = dict.get(s.toCharArray()[0]);
String data = s.split(".")[1];
String code = name + " x = new " + name + "(data);";
SYS.exec(code); //???
...
}

I'm not sure I understand correctly, seems weird no one else mentioned this yet:
Map<Character, ShapeFactory> dict = new HashMap<>();
dict.put('C', new CircleFactory());
dict.put('R', new RectangleFactory());
dict.put('T', new TriangleFactory());
...
ShapeFactory factory = dict.get(symbol);
Shape shape = factory.create(data);

You can make use of reflection to create instance dynamically.
String className = "com.shape.Triangle";
Class classDefinition = Class.forName(className);
Object obj = classDefinition.newInstance();
Or
Just use if-else to create instance of specific class.

Exec in Python executes code.
You can do the same thing with Java, for example with javassist.
You could read the data file, compile the statements and insert them in your own class.
But it seems overkill.
You could also use java reflection but it will produce a brittle and unclear code.
Instead if else if, I think that you should use abstraction and create a factory class by type of object.
It could look like :
Scanner file = new Scanner (new File("shapes.dat"));
String s;
Map<Character, ShapeBuilder> dict = new HashMap<Character,String>();
dict.put('C', new CircleBuilder());
dict.put('R', new RectangleBuilder());
dict.put('T', new TriangleBuilder());
while (file.hasNextLine()){
s = file.nextLine().trim();
char shapeSymbol = ...; // computed from s
ShapeBuilder builder = dict.get(shapeSymbol);
Shape shape = builder.build(s);
}

You can actually use polymorphism to avoid if-else statements. So, you can create objects that actually do those two jobs you want, match a line and create a shape. So you could use something like the following code.
public class Program {
public static void main() throws FileNotFoundException {
Scanner file = new Scanner(new File("shapes.dat"));
while (file.hasNextLine()) {
String line = file.nextLine().trim();
Shape shape = new Matches(
new RectangleMatch(),
new TriangleMatch(),
new SquareMatch(),
new CircleMatch()
).map(line);
}
}
public interface ShapeMatch {
boolean matches(String line);
Shape shape(String line);
}
public static final class RectangleMatch implements ShapeMatch {
#Override
public boolean matches(String line) {
return line.startsWith("R");
}
#Override
public Shape shape(String line) {
String[] dimensions = line.substring(2).split(" ");
return new Rectangle(
Integer.parseInt(dimensions[0]),
Integer.parseInt(dimensions[1]),
Integer.parseInt(dimensions[2])
);
}
}
public static final class CircleMatch implements ShapeMatch {
#Override
public boolean matches(String line) {
return line.startsWith("C");
}
#Override
public Shape shape(String line) {
return new Circle(Integer.parseInt(line.substring(2, line.indexOf(" "))));
}
}
public interface ShapeMapping {
Shape map(String line);
}
public static final class Matches implements ShapeMapping {
private final Iterable<ShapeMatch> matches;
public Matches(ShapeMatch... matches) {
this(Arrays.asList(matches));
}
public Matches(Iterable<ShapeMatch> matches) {
this.matches = matches;
}
#Override
public Shape map(String line) {
for (ShapeMatch match : matches) {
if (match.matches(line)) {
return match.shape(line);
}
}
throw new RuntimeException("Invalid shape entry line.");
}
}
}

Related

Adding elements to list using getter/setter

Every time I call the method inserimentoVoto to add elements in a list contained in the object Studente, the data is overwritten I know it's easy but I just started to code.
public class Run {
public static void main(String[] args) {
Gestione g = new Gestione();
Studente s = new Studente();
g.inserimentoVoto(s);
}
}
This is the method
public void inserimentoVoto(Studente s) {
Voto v = new Voto();
System.out.println("Insert value");
v.setVoto(scanner.next());
System.out.println("Insert name");
v.setMateria(scanner.next());
v.setDataVoto(new Date());
s.setListaVoti(new ArrayList<Voto>());
s.getListaVoti().add(v);
}
s.setListaVoti(new ArrayList<Voto>());
You are creating a new ArrayList everytime
The above line should be only done once in the Studente class.
public class Studente
{
private ArrayList<Voto> arr = new ArrayList<Voto>();
... Other data ...
public ArrayList<Voto> getListaVoti()
{
return arr;
}
... Other methods ...
}
You do not need a setListaVoti at all - because it's done only once.
In the inserimentoVoto method, you only need
s.getListaVoti().add(v);

Check duplicate variable in a method scope Java

I read java file using FileReader that contains some method. How i can read method scope (method area inside) to find duplicate variable?
For example, this is a java file that i read:
public double[] copyArray(double[] data) {
int _nn = data.length;
double[] _tmp = new double[_nn];
System.arraycopy(data, 0, _tmp, 0, _nn);
int _nn;
_nn = tmp;
return _tmp;
}
How to know method scope?, i mean between { and }, if scope has found, find duplicate variable such as example above (int _nn) duplicated.
[addition]
i have tried using java parser, then is success. Then i should send the results using list, but only last method in the list. What's wrong with my code?
This is a MethodVisitor:
private static class MethodVisitor extends VoidVisitorAdapter {
private List<String> list = new ArrayList<String>();
#Override
public void visit(MethodDeclaration n, Object file) {
list.add(n.getName());
}
public List<String> getList() {
return list;
}
}
Then, this is a method to call MethodVisitor:
private MethodVisitor mv;
public void doIt(File file) throws Exception {
CompilationUnit cu;
try {
// parse the file
cu = JavaParser.parse(file);
} finally {
//file.close();
}
// visit and print the methods names
mv = new MethodVisitor();
mv.visit(cu, file);
List<String> list = mv.getList();
for(String item:list){
System.out.println(item);
}
}
1 you need to parse java code:
http://code.google.com/p/javaparser/
or read this:
Java source code parsers/generators
2 after that, try something and show your code

Different variants of method depending on parameters

I have classes called ctdl_User, ctdl_Device and ctdl_Options.
I have a function that saves ctdl_User objects using the binary formatter, and another that loads them. However the functions specifically expect to take and return User objects, and I want to use this function to load other objects of mine.
How do I go about changing what types the functions will take? Here is the save and load functions...
public ctdl_User Load()
{
ctdl_User loadedUsr = new ctdl_User();
string DataFileSave = Settings.Default.savePath + "\\testuserfile.dat";
FileStream dataStr = new FileStream(DataFileSave, FileMode.Open);
BinaryFormatter frmtr = new BinaryFormatter();
loadedUsr = (ctdl_User) frmtr.Deserialize(dataStr);
dataStr.Close();
return loadedUsr;
}
public static void Save(ctdl_User usr)
{
string DataFileSave = Settings.Default.savePath + "\\testuserfile.dat";
File.Delete(DataFileSave);
FileStream dataStr = new FileStream(DataFileSave, FileMode.Create);
BinaryFormatter frmtr = new BinaryFormatter();
frmtr.Serialize(dataStr, usr);
dataStr.Close();
}
The following demonstrates an approach using generics in C#:
public static T Load<T>() where T : new()
{
T loadedUsr = new T();
string DataFileSave = Settings.Default.savePath + "\\testuserfile.dat";
FileStream dataStr = new FileStream(DataFileSave, FileMode.Open);
BinaryFormatter frmtr = new BinaryFormatter();
loadedUsr = (T) frmtr.Deserialize(dataStr);
dataStr.Close();
return loadedUsr;
}
public static void Save<T>(T usr)
{
string DataFileSave = Settings.Default.savePath + "\\testuserfile.dat";
File.Delete(DataFileSave);
FileStream dataStr = new FileStream(DataFileSave, FileMode.Create);
BinaryFormatter frmtr = new BinaryFormatter();
frmtr.Serialize(dataStr, usr);
dataStr.Close();
}
Note the use of the new() constraint in the Load() method.
Here's an example of calling these methods:
public static void Main(string[] args)
{
ctdl_User user = new ctdl_User();
user.name = "Alice";
Save<ctdl_User>(user);
ctdl_User user2 = Load<ctdl_User>();
Console.WriteLine(user2.name);
ctdl_Device device = new ctdl_Device();
device.type = "printer";
Save<ctdl_Device>(device);
ctdl_Device device2 = Load<ctdl_Device>();
Console.WriteLine(device2.type);
}
For completeness, here are the stub classes I used to test this code:
[Serializable()]
class ctdl_User
{
public string name;
}
[Serializable()]
class ctdl_Device
{
public string type;
}
Edit: added code to Main() that saves and loads a device, as well as a user.
Assuming this is Java you can just overload the method following this scheme:
public static void Load(Type1 obj)
{
//do sth with object of type Type1
}
public static void Load(Type2 obj)
{
//do sth with object of type Type2
}
//...etc
Also if you have common code for it that can be used for objects of another types you can exrtact this code to one methode that treat overloaded methods as facade
private static void doLoad(Object obj)
{
//the common code
}
public static void Load(Type1 obj)
{
doLoad(obj); // or something else...
}
public static void Load(Type2 obj)
{
doLoad(obj); // or something else...
}
//...etc
You can read more about overloading methods in Java here

Use the command line to make new objects

In my program, the user needs to input what type of players the game will have. The players are "human", "good" (for a good AI), "bad" (for a bad AI) and "random" (for a random AI). Each of these players have their own class that extend one abstract class called PlayerType.
My struggle is mapping a String to the object so I can A) create a new object using the String as sort of a key and B) get the related String from an object of its subclass
Ultimately, I just want the implicit String to only appear once in the code so I can change it later if needed without refactoring.
I've tried using just a plain HashMap, but that seems clunky with searching the keys via the values. Also, I'm guessing that I'll have to use the getInstance() method of Class, which is a little less clunky, which is okay if it's the only way.
What I would do is create an enum which essentially functions as a factory for the given type.
public enum PlayerTypes {
GOOD {
#Override
protected PlayerType newPlayer() {
return new GoodPlayer();
}
},
BAD {
#Override
protected PlayerType newPlayer() {
return new BadPlayer();
}
},
RANDOM {
#Override
protected PlayerType newPlayer() {
return new RandomPlayer();
}
};
protected abstract PlayerType newPlayer();
public static PlayerType create(String input) {
for(PlayerTypes player : PlayerTypes.values()) {
if(player.name().equalsIgnoreCase(input)) {
return player.newPlayer();
}
}
throw new IllegalArgumentException("Invalid player type [" + input + "]");
}
)
Because then you can just call it like so:
String input = getInput();
PlayerTypes.create(input);
Of course, you'll get an IllegalArgumentException which you should probably handle by trying to get the input again.
EDIT: Apparently in this particular case, you can replace that loop with just merely
return PlayerTypes.valueOf(input).newPlayer();
And it'll do the same thing. I tend to match for additional constructor parameters in the enum, so I didn't think of using valueOf(), but it's definitely cleaner.
EDIT2: Only way to get that information back is to define an abstract method in your PlayerType class that returns the PlayerTypes enum for that given type.
public class PlayerType {
public abstract PlayerTypes getType();
}
public class GoodPlayer extends PlayerType {
#Override
public PlayerTypes getType() {
return PlayerTypes.GOOD;
}
}
I like the answer provided by Epic but I don't find maps to be clunky. So it's possible to keep a map and get the constructor call directly.
Map<String, Supplier<PlayerType> map = new HashMap<>();
map.put("human", Human::new);
Human h = map.get("human").get();
The two main options I can think of:
Using Class.newInstance(), as you mentioned (not sure if you had this exact way in mind):
// Set up your map
Map<String, Class> classes = new HashMap<String, Class>();
classes.put("int", Integer.class);
classes.put("string", String.class);
// Get your data
Object s = classes.get("string").newInstance();
You could use Class.getDeclaredConstructor.newInstance if you want to use a constructor with arguments (example).
Another option is using switch:
Object getObject(String identifier) {
switch (identifier) {
case "string": return new String();
case "int": return new Integer(4);
}
return null; // or throw an exception or return a default object
}
One potential solution:
public class ForFunFactory {
private ForFunFactory() {
}
public static AThing getTheAppropriateThing(final String thingIdentifier) {
switch (thingIdentifier) {
case ThingImplApple.id:
return new ThingImplApple();
case ThingImplBanana.id:
return new ThingImplBanana();
default:
throw new RuntimeException("AThing with identifier "
+ thingIdentifier + " not found.");
}
}
}
public interface AThing {
void doStuff();
}
class ThingImplApple implements AThing {
static final String id = "Apple";
#Override
public void doStuff() {
System.out.println("I'm an Apple.");
}
}
class ThingImplBanana implements AThing {
static final String id = "Banana";
#Override
public void doStuff() {
System.out.println("I'm a Banana.");
}
}

Get null value from another object in Java, but get value in own class

When I try to execute this code, after I choose AMD, I got null in value. how it can be happen ?
below is the source code :
[for main]
public class processor{
public int hargapro;
public String nmbarangpro;
public static final Scanner input = new Scanner(System.in);
public String getpro()
{
return nmbarangpro;
}
public int getproharga()
{
return hargapro;
}
public void daftarpro() {
List<String> daftarpro = new ArrayList<>();
daftarpro.add("AMD");
daftarpro.add("Intel");
List<String> nomer = new ArrayList<>();
nomer.add("1. ");
nomer.add("2. ");
System.out.println("Processor yang tersedia :");
for (int i = 0; i < daftarpro.size(); i++) {
System.out.println(nomer.get(i)+daftarpro.get(i));
}
System.out.println("Pilihan anda : ");
int pilih = input.nextInt();
switch(pilih)
{
case 1:
{
System.out.println("Anda membeli Processor AMD");
System.out.println("Seharga Rp 1.200.000");
harga(1200000); //call harga method
namabarang("AMD"); //call namabarang method
System.out.println(getpro()); //[for testing]filled with AMD[ni problem here]
System.out.println(getproharga()); //[for testing][filled with 1200000[no problem here]
break;
}
case 2:
{
System.out.println("Anda membeli Processor AMD");
System.out.println("Seharga Rp 1.200.000");
harga(1500000);
namabarang("Intel");
break;
}
default:
System.out.println("Pilihan tidak tersedia");
daftarpro();
}
}
#Override
public int harga(int hargamasuk) {
return hargapro = hargamasuk;
}
#Override
public String namabarang(String barang) {
return nmbarangpro = barang;
}
public static void main(String[] args) {
processor a = new processor();
a.daftarpro();//get menu from daftarpro()
kasir x = new kasir();
x.semua();//get null in value
}
}
my second files :
public class kasir {
public void semua()
{
processor a = new processor();
System.out.println(a.getpro());
}}
When I try to read value through class kasir, i get x.semua filled with null value. how it can be happen ?
Your semua method creates a new instance of processor which it then reads from:
public void semua()
{
processor a = new processor();
System.out.println(a.getpro());
}
That's entirely unrelated to the processor instance you've created in your main method. If your kasir class should logically "know about" the other processor instance, you probably want a processor field in the class, which you might populate via the constructor - so your main method might become:
public static void main(String[] args) {
processor a = new processor();
a.daftarpro();
kasir x = new kasir(a);
x.semua();
}
As an aside, you should really try to follow the Java naming conventions, so classes of Processor and Kasir, and methods of getPro etc. (And if your code actually looks like that in your editor, I suggest you reformat it, too...)

Categories

Resources