- Objects
- Classes
- Inheritance
-
Programming in Java on Your Computer - Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Trying out Code in BlueJ
Premium Content - Free Preview
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 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)
End of Free Content Preview. Please Sign in or Sign up to buy premium content.
Comments
Victoria Holland
Jan 16, 11:53 AMI'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 {
public static void main(String args[]) {
int sum = 0;
int square = 0;
for (int i = 1; i<=50; i++)
{
if(i%2 != 0)
{
System.out.println("i is an odd number: " + i);
square = i * i;
System.out.println("The square of i is: " + square);
sum = sum + square;
System.out.println("The sum so far is: " + sum);
}
}
System.out.println(sum);
}
}
Learneroo
Jan 16, 12:30 PM@Victoria, 1 to 50 only contains 26 odd numbers (see the hint).
Victoria Holland
Jan 17, 4:21 AMThanks, 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.
jack
Mar 11, 8:39 PM166650
thales
Jul 8, 10:23 AMThis worked just fitne
int sum=0;
for (int i=1;i<100;i=i+2){sum=sum+i*i;}
System.out.println(sum);
Bolke
Jul 30, 6:11 PMIt just keeps saying: Error, ";" expected