Trying out Code in BlueJ


Collapse Content

While the previous node demonstrated trying things out with BlueJ's visual interface, this node will cover trying things out in code.

Trying Out Code
It is often convenient to quickly try out code without having to put it into a whole program and compile it. However, since Java is a Compiled Language, it doesn't come with a built-in "tryer-outer". BlueJ solves this issue by providing a CodePad for this purpose.

The Codepad
the-codepad

The codepad should be visible on the bottom right corner of BlueJ. If it isn't, click on View > Show Codepad.

Using the Codepad
To evaluate an expression in Java, just type it into the codepad and hit "enter" . Don't put a semicolon at the end of the line. For example:

1+1
2   (int)
"hello".length()
 5   (int) 

You can also access objects that were created earlier in the codepad, or on the Object bench. For example, if you created a Student student1 earlier, you could type:

student1.getStudentID()
 "ID 00234"   (String)

To Execute a Statement in Java, just type it normally into the codepad with a semicolon at the end. For example:

int n = 3 + 2;
System.out.println("hello");

The first line will assign 5 to the variable n, so n could then be used in another line in the codepad. The second line will open up the terminal and display "hello". (To open the terminal on your own, go to View > Show Terminal). The next node will discuss the terminal some more.

To Enter a Multi-line Block of code, press Shift-Enter after each line so it doesn't execute the code in the middle. For example, in the code below, you would need to press "Shift-Enter" after the 1st and 2nd line, and "Enter" after the last line.

 if(n>3){ 
  System.out.println("hoorah"); 
}

Inspecting Objects
To inspect Objects in the codepad, you can double-click on the Object icon on the side. You can also drag the Object into the object bench.

Toggle animation

Try the codepad
The main use of the codepad is to quickly try out a line of code or see what a certain method does. It can also be useful if you want to quickly run some code to get a certain result and you won't need that code again. See if you can solve the challenge below with some quick code in your codepad.

Note: To write multiple lines of code in the codepad you can press ctrl-enter. This will let you start a new line of code without executing the current line.
Since you're just doing some quick code, you can put multiple lines of code on one line since whitespace doesn't matter in Java, just the correct syntax (e.g. ;, {} and ()). In general though, you should keep your code clear and properly spaced for readability.

Challenge

What is the sum of the squares of the first 50 odd numbers (under 100)?

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • I'm stumped on this one. I got the answer 20825, but it says this is incorrect. Maybe I misinterpreted the question? This is my code (compiled and verified in an IDE):
    class Example {

    cont...
  • @Victoria, 1 to 50 only contains 26 odd numbers (see the hint).

  • Thanks, I've got it now. I may well take up the offer of membership in future - I'll see how I get on for now.

  • 166650

  • This worked just fitne

    int sum=0;
    for (int i=1;i<100;i=i+2){sum=sum+i*i;}
    System.out.println(sum);

  • It just keeps saying: Error, ";" expected

Contact Us
Sign in or email us at [email protected]