Draw a crate with Java

In this post we are going to draw a crate. This is a quiz that I had to solve for the course I am taking at Udacity.com; Introduction to programming. Thanks to professor Cay Horstman for making this assigment simple by constructing a simple graphic library that we will be using to solve this problem.

Download the java graphic library here:

Simple graphic library


We need to draw the following crate:



// Draw a crate by combining a rectangle with five lines.
// The front face is a rectangle with top left corner (20, 30),
// width 100, and height 40.
// A diagonal line goes from the face’s top left corner to (50, 10).
// Another diagonal line goes from the face’s bottom right corner
// to (150, 50).
// A third diagonal starts at the face’s top right corner and is
// parallel to the others.
// The fourth and fifth line join the end points of the diagonals.
// Before programming, get out a pen and draw the crate on a
// sheet of paper! Label each of the points with their coordinates.
// Also, don’t forget to call draw() on your rectangle and line objects.



public class DrawACrate
public static void main(String[] args)
        Rectangle face = new Rectangle(20, 30, 100, 40);
        Line line1 = new Line(20, 30, 50, 10);
        Line line2 = new Line(120, 30, 150, 10);
        Line line3 = new Line(50, 10, 150, 10);
        Line line4 = new Line(150, 50, 150, 10);
        Line line5 = new Line(120, 70, 150, 50);

You can try it at Udacity: Here



Copying strings in Java

This exercise took me awhile to figure out. It is very simple, but what made it harder was the question being asked. I think my best advice is to be very careful about what’s being asked. In this exercise I create two variables, the second variable holds the value of the first variable, then I needed to make the second variable uppercased. 

The code:

String greeting = “Hello, World!”;

String greeting2 = greeting;


The questions:

What is the value of greeting?

What is the value of greeting2?

Here is a more visual presentation:



Compiling and then running the above 3 lines of Java will only returns the current value of greeting2, which is in uppercase because we are calling the toUpperCase() method on greeting2, currently. This is where I got confused, when I ran the code the result was “HELLO, WORLD!” and I was getting it wrong because that’s not what’s being asked.

In order to find out what the answer to the aforementioned questions are, one have to call greeting and greeting2 indepandently, or if you’re smarter than me you can figure it out immediately. 



Once we call greeting2 indepent from the toUpperCase() method we get “Hello, World!”, which is the value of the variable greeting (the first variable) and will not return the value uppercased because greeting2 is just a reference to the value of the first variable.

So the answer to both of the obove questions are the same, because on the second question they are asking what’s the value of greeting2 and not what’s the value of greeting2.toUpperCase().

Java replacing a character within a string

For the last few hours I’ve been following up on the Introduction to programming course at Udacity and thought that it my be helpful to review what I’ve been learning so far by writing about it. That’s one of the reasons why I started this blog. I think that writing about what I’m currently self-studying can make learning more efficient.

The Java Replace Method

For this exercise I need to:


Hints that were given:

Go to  the replace() method in the javadoc
Verify your answer using BlueJ
Here is an example method call: birthday.getMonth();

To solve the problem I created a new java file with the following code:

String river = “Mississippi”; // Initialize a string variable

System.out.println(river.replace(“i”, “x”); // Replace the letter “i” with the letter “x” by using the replace method

And finally the keyword String before the variable name (in this case river) declares the return type which is string

The result is: Mxssxssxppx