2
\$\begingroup\$

The following code is my solution of exercise 10 of chapter 5 of The Art and Science of Java. So here, using acm program and graphics, I need to draw a castle with two side towers and other three consecutive towers some distance away from the main building(castle and its two side towers). Is there any way to do this faster than that?

(The drawing is perfect as I spent plenty of time searching for the perfect location for every object on the canvas.)

import acm.program.*;
import acm.graphics.*;

public class Hw1 extends GraphicsProgram {

final double tower_width = 150;
final double tower_height = 600;
final double main_house_width = 400;
final double main_house_height = 500;
final double s_tower_width = 75;
final double s_tower_height = 300;
final double door_width = 50;
final double door_height = 100;
final double window_width = 100;
final double window_height = 100;

GRect create_tower(double x, double y) {
    GRect rect = new GRect(tower_width, tower_height);
    rect.move(x, y);
    return rect;
}

GRect create_main_home(double x, double y) {
    GRect rect = new GRect(main_house_width, main_house_height);
    rect.move(x, y);
    return rect;
}

GRect create_s_tower(double x, double y) {
    GRect rect = new GRect(s_tower_width, s_tower_height);
    rect.move(x, y);
    return rect;
}

GRect create_door(double x, double y) {
    GRect rect = new GRect(door_width, door_height);
    rect.move(x, y);
    return rect;
}

GOval create_window(double x, double y) {
    GOval circle = new GOval(window_width, window_height);
    circle.move(x, y);
    return circle;
}

GPolygon create_arch(double x, double y) {
    GPolygon arch = new GPolygon();
    arch.addVertex(x, y);
    arch.addVertex(x+150, y);
    arch.addVertex(x+75, y-150);
    return arch;
}

GPolygon create_s_arch(double x, double y) {
    GPolygon arch = new GPolygon();
    arch.addVertex(x, y);
    arch.addVertex(x+75, y);
    arch.addVertex(x+37.5, y-100);
    return arch;
}

GPolygon create_arch_door(double x, double y) {
    GPolygon arch = new GPolygon();
    arch.addVertex(x, y);
    arch.addVertex(x+50, y);
    arch.addVertex(x+25, y-50);
    return arch;
}

GPolygon create_house_arch(double x, double y) {
    GPolygon arch = new GPolygon();
    arch.addVertex(x, y);
    arch.addVertex(x+400, y);
    arch.addVertex(x+200, y-200);
    return arch;
}

public void run() {

    add(create_tower(700, 250));
    add(create_tower(150, 250));
    add(create_main_home(300, 350));
    add(create_s_tower(1000, 550));
    add(create_s_tower(1200, 550));
    add(create_s_tower(1400, 550));
    add(create_door(475, 750));
    add(create_window(350, 450));
    add(create_window(550, 450));
    add(create_arch(150, 250));
    add(create_arch(700, 250));
    add(create_s_arch(1000, 550));
    add(create_s_arch(1200, 550));
    add(create_s_arch(1400, 550));
    add(create_arch_door(475, 750));
    add(create_house_arch(300, 350));
}

}
\$\endgroup\$
  • \$\begingroup\$ Doesn't THE ART AND SCIENCE OF JAVA teach to use camel case for method names? And upper case for constants? If it really doesn't, try a different book. And as you introduced constants for the object geometries, you should have eliminated the x+400 or y-200 expressions as well. \$\endgroup\$ – Ralf Kleberhoff Jan 15 '18 at 12:25
  • \$\begingroup\$ @RalfKleberhoff Propably, it talks about camel case etc... and propably i forgot to follow this rule when i wrote this code or propably i didn't care or propably i don't know what...I don't think that answers my question though. \$\endgroup\$ – Κωνσταντίνος Κορναράκης Jan 15 '18 at 12:34
0
\$\begingroup\$

There is not much to optimize but the following will still speed up things:

final double tower_width = 150;
-------------------------------------------------- Compiler can fill in better
private static final double TOWER_WIDTH = 150;

And

GRect create_tower(double x, double y) {
-------------------------------------------------- Compiler can inline method body
private GRect createTower(double x, double y) {

Stylistic:

public void run() {
-------------------------------------------------- Code Style, detects misspelling
@Override
public void run() {

Real optimisations can be done, by the variations offered by the GObject API:

GRect create_main_home(double x, double y) {
    GRect rect = new GRect(main_house_width, main_house_height);
    rect.move(x, y);
    return rect;
}
-------------------------------------------------- Optimised API usage
GRect createMainHome(double x, double y) {
    return new GRect(x, y, MAIN_HOUSE_WIDTH, MAIN_HOUSE_HEIGHT);
}

I took the liberty to use the java style that is quite a hard convention in java (so I do not get downvoted).

Questionable is whether to use int or float instead of double. I would leave it at double.

\$\endgroup\$
  • \$\begingroup\$ I agree with your corrections in my code, i could have saved quite many lines in my code that way. I am not really sure if i get the reason you added @Override before public void run(). \$\endgroup\$ – Κωνσταντίνος Κορναράκης Jan 15 '18 at 13:56
  • \$\begingroup\$ My assumption on the functioning of the code, is that you have to implement run. I have seen some errors, where @Override was left out, but the function signature had a small error: a wrong parameter class. Suppose you would have written rum instead of run (psychologically telling). Then you would wonder why the function was not called. \$\endgroup\$ – Joop Eggen Jan 15 '18 at 14:09
1
\$\begingroup\$

here are my comments:

1) instead of having a create method for objects, why not have classes that you can instantiate? seems more clear to me

public class Tower extends GRect {
  final double width = 150;
  final double height = 600;
  public Tower() {
    super(width, height);
  }
}

2) instead of creating a GRect and then moving it, you can specify the location when you add it: add(new Tower(), 700, 250); this shows more clearly the intention of placing the tower at a certain location

\$\endgroup\$
  • \$\begingroup\$ I agree about your second comment... The problem is that I am not allowed to use super(), so even if your solution for my problem is right, I can't implement what exercise says in this way. Useful, answer thanks! \$\endgroup\$ – Κωνσταντίνος Κορναράκης Jan 15 '18 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.