Back to Back Issues Page
Fresh Cup of Java, Issue #001 -, welcome to the first issue!
October 02, 2010
Hello,

# Welcome to the First Fresh Cup of Java Issue!

Congratulations on receiving our very first issue of our Java newsletter, Fresh Cup of Java! In this issue we'll be looking at some of the top frequently asked questions, including lots of pyramid examples, a unique guide on how to think like a programmer and how to improve your logical thinking, special previews of upcoming sections of the site, sample quiz questions and their answers, plus a sample calculator program with graphics!

Table of Contents:

1. Answers to frequently asked questions
• How to create a centered pyramid
• Create a pyramid of numbers
• Create a pyramid with 12321 type pyramid structure
• How to add numbers with Scanner
2. How to think like a programmer  An intro to logical thinking
3. New updates to the site + previews
4. Bonus sample program  Calculator with graphics

## Answers to Frequently Asked Questions

Here are some of the most common questions asked on the site. Hopefully you find some value to the answers presented here!

A. How to create a centered pyramid

first of all, I'll assume you've looked at our Java Pyramids example. If so, then you know how to make a pyramid on the left like this:

for (int x = 1; x <= 5; x++) {
for (int y = 1; y < x+1; y++) {
System.out.print("*");
}
System.out.println();
}

So, how do we make it centered? It's simple, you just have to remember that to center a pyramid, you have to create a pyramid of spaces on the left as well as your regular pyramid!

for (int x = 1; x <= 5; x++) {
for (int y = 5; y > x; y--) {
System.out.print(" ");
}

for (int y = 1; y < x+1; y++) {
System.out.print(" *");
}
System.out.println();
}

Notice how we made a new for loop, one that counts down from 5, and goes to the size of x, to get a backwards pyramid of spaces. Your pyramid will now look like this:

*
**
***
****
*****

So, the last thing we need to do is center the pyramid! How? Just add a space in front of the * symbol when you print it, like this:

System.out.print(" *");

That will give you a centered pyramid!

B. How to create a pyramid of numbers

Again, it is very important that you've looked up our Java Pyramids example ! If so, then there is very little more that you have to do to change the stars to numbers.

We can have the pyramid start counting from 1, 1 2, 1 2 3, etc. by replacing what we print out inside of the loop. Like so:

for (int x = 1; x <= 5; x++) {
for (int y = 1; y < x+1; y++) {
System.out.print(y);
}
System.out.println();
}

See how * was replaced with just y? Now we are printing the value of y, which always resets back to 1 and goes up to one more than what x equals, which is exactly what you want! Remember not to put quotes around y, because we do not want a pyramid of y, we want to print out the value of y at each step of the loop.

C. Create a pyramid with 12321 type pyramid structure

This type of a pyramid is tricky. Basically, the numbers have to count up and then count back down for each row. It will look something like this in the end:

1
232
34543
4567654
567898765
67890109876
7890123210987
890123454321098
90123456765432109
0123456789876543210

It looks like a complicated pyramid, and in a way it is. But, let's see how we need to think about the solution to this problem. This pyramid will require that we center it. Refer to the first FAQ to see how to center a pyramid. You can use that code to come up with the solution for this pyramid.

We'll need to change our print statement because we are now printing out numbers. We could just try printing out the variable y, but you can quickly see how that wont work (Give it a try to see what that does!). We probably need to have something with x, so let's try y + x because the number you start with changes depending on what row you are on, and since x counts rows, it makes sense to use it.

public static void main(String...args) {
for (int x = 1; x <= 10; x++) {
for (int y = 10; y > x; y--) {
System.out.print(" ");
}
for (int y = 1; y < x+1; y++) {
System.out.print(" " + (y+x));
}
System.out.println();
}
}

So, you'll notice the numbers are off by one, that's ok, just subtract one from y+x. Notice how I have parenthesis around the math. This is because otherwise it doesn't know to add those variables together and will try to use + like you do with Strings!

You'll quickly notice that you have numbers going to 10, 11, 12, just use an if statement to check if the number is bigger than 9. If it is, subtract 10. That's all.

Now you're almost done, all you need to do is to create the numbers that count backwards. For this, we'll need ANOTHER for loop to make it simple. We'll use variable z this time. Remember that to count backwards, you want to start with the last number you printed out and subtract one from that. You also need to count backwards, and on top of that you need to make as many numbers as the row you are on minus 1. Think you can do that? Here's some hints:

1. You need to create y outside of the 2nd loop, like this:

int y = 0;
for (y = 1; y < x+1; y++) {
if (y+x -1 > 9) {
System.out.print(" " + (y+x-11));
}
else {
System.out.print(" " + (y+x-1));
}
}

2. y+x-1 was the last number you print on a line going forwards. So, after the loop increments one more time before exiting, you have y+x. What does this number need to be subtracted by to start your backwards count?

3. You will still need the if-else for when your variable z is greater than 9, just like before.

I highly recommend you attempt to code this on your own. HOWEVER, if you get frustrated with this, you can now view the full solution below:

public static void main(String...args) {

for (int x = 1; x <= 10; x++) {

for (int y = 10; y > x; y--) {

System.out.print(" ");

}

int y = 0;

for (y = 1; y < x+1; y++) {

if (y+x -1 > 9) {

System.out.print(" " + (y+x-11));

}

else {

System.out.print(" " + (y+x-1));

}

}

for (int z = y+x-3; z > x-1; z--) {

if (z > 9) {

System.out.print(" " + (z-10));

}

else {

System.out.print(" " + (z));

}

}

System.out.println();

}

}

It's always important to breakdown problems. Solving a problem one piece at a time is a lot easier than trying to come up with all of the code at once. And of course, never be afraid to experiment a little bit and to keep tweaking code until you get it right!

D. How to add numbers with Scanner

Actually, our Java Scanner page has the code needed to add numbers using Scanner! However, this question does get asked A LOT, so we'll go over it.

All a Scanner does it read input. It doesn't matter from where, but for our example, we'll have the Scanner read from System.in . System.in is the input from a user's keyboard in the console. To set this up, we only need one line:

Scanner scan = new Scanner(System.in);

There, we now have a scanner for reading user input. How will we add two numbers that a user wants? Well first, we should probably ask the user to input the first number:

System.out.print(Enter the first number: );

Next, we actually have to get the number that the user will type in:

int num1 = scan.nextInt();

nextInt() is what you want to use to grab integers. If you want it to be a decimal number, you'll have to use nextDouble() instead.

Ok, after that, we ask the user for the second number and get their input:

System.out.print(Enter the second number: ); int num2 = scan.nextInt();

Last, we just print out the result:

int answer = num1 + num2; System.out.println(The answer is  + answer);

That's all there is to it! You can put a while loop around the whole program so it continues to ask for numbers over and over again if you want. Just make sure that the line where you create the Scanner is outside of the loop, because you only need to create the Scanner once.

## How to think like a programmer  An intro to logical thinking

As you might probably have guessed, programming is a big exercise in logical thinking. A program is literally a set of instructions you are giving the computer to execute. The hardest part is not coming up with stuff to tell the computer to do, the hardest part is coming up with the instructions necessary for the computer to do what you want it to do. For this short little piece we're going to look at how the computer executes your Java code.

This is the most important thing to understand about your Java program when the computer is running it, ready?

The computer can only execute your program one line at a time.

That's right, no matter how fast the computer is, it can still only read one line at a time. Which brings up to another excellent point:

The computer starts reading from the top, and works it way to the bottom

The computer reads your code in order from top to bottom, even if you feel there is not much order to your code at all.

Given these two conditions then, how do we create sophisticated programs? The answer lies within some of the tools programming languages provide to us. For example, if statements help to prevent certain code from executing (since the computer goes from top to bottom). Or, if you want to prevent yourself from writing code over and over again, or you have a piece of code that needs to execute over and over again, you can use a for loop or a while loop.

So ok, now the trick is to figure out when you need a for loop, or when you need an if statement. For loops I feel are the hardest to understand. Let's take the pyramid example:

*
**
***
****
*****

When you look at the pyramid, you don't see the concept of loops or if statements at all. All you see are stars in a pyramid pattern. So, how do we think of this as a computer would? The first step is to identify things that repeat, patterns. So, what is one pattern we see? The first pattern to immediately see is that there are 5 lines of stars. By lines, I mean there are 5 rows of stars. So, from the computer's standpoint, it will need to print out a line of stars 5 times. Note that we're not considering how many stars to print on each line, simply that we have to print out 5 lines. How do we repeat the same code of printing out a line of star(s)? Using a for loop. That's how you know to create a for loop that loops 5 times.

The next pattern to notice is that the number of stars printed out on a line depends on the line number. On line 1, we print out 1 star. On line 2, we print out 2 stars. This keeps going up to line 5, where you print out 5 stars. So, we have a loop that prints lines of stars, but within that loop, we also need to vary the number of stars printed. The best way to look at it is that the computer is printing out a single star over and over again, and the number of times it repeats is only based on the line (or row) that we are on. If you take a look at the code for the pyramid, you should be able to see that the for loops do exactly that. Try tracing the code. Use your finger and go through the code one line at a time. Write down any output the computer would normally do yourself. You should be able to reproduce exactly what the computer does.

## New updates to the site + previews

If you haven't been to the site lately then there are a few new things you've missed.

First, there's been a change to the left sidebar. It's now categorized so it should make finding what you are looking for a whole lot easier! Hopefully it's been a great help for finding resources.

Second, there are two new tutorials out for the Advanced Java Tutorials section.

The first tutorial deals with polymorphism. Polymorphism and its concept is one of the key features of object-oriented programming. Without understanding this key concept, you will never truly grasp Java as an object-oriented language. There's plenty of good examples inside, and I highly recommend checking it out! You can read this tutorial by clicking here.

The second tutorial is a must read for those who are having difficulties with Java not recognizing that your variables exist. Heck, maybe you're having difficulty figuring out where to define your variables. That's okay, because most courses that teach Java just don't cover the concept of variable scope very well, if at all. This tutorial does cover variable scope, and is worthwhile to read if you're having trouble with variables. You can read this tutorial by clicking here.

So, without further adieu, some new things coming soon to the site:

The question/comment forms from each tutorial will be removed and replaced with a friendly reminder that you can ask questions via the contact us form on the Contact Us page. This is not a big change, and it'll help with the mess I get on the email side of things. Hopefully this will not be too much of a big deal and the useful feedback will keep on coming.

The Advanced Tutorials are almost done! There is one last lesson, Generics, which hopes to explain why things like ArrayList have

Back to Back Issues Page