java project
Let's work through an example of creating a new BlueJay project and writing a Java program.
This example will be similar enough that you should be able to take the techniques learned here and apply them to the required project tasks.
This example will be similar enough that you should be able to take the techniques learned here and apply them to the required project tasks.
creating a bluejay project and writing a java program
Find where you installed BlueJay and run it. I can run it from the Start Menu shown here.
Note: At some point BlueJay may ask you to share your usage information with the creators of BlueJay. This is non-personal information about how you are using their software. Most major companies ask for this kind of thing, so it's nothing to worry about. But I have two points:
1) It's optional, so I usually click No Thanks.
2) If you are not at least 16 years old, you are not allowed by law to share your information.
So just click No Thanks.
Note: At some point BlueJay may ask you to share your usage information with the creators of BlueJay. This is non-personal information about how you are using their software. Most major companies ask for this kind of thing, so it's nothing to worry about. But I have two points:
1) It's optional, so I usually click No Thanks.
2) If you are not at least 16 years old, you are not allowed by law to share your information.
So just click No Thanks.
This is what you should see when BlueJay launches. The first thing we need to do is create a new project.
To create a new Project, go to Project -> New Project...
This part is a little tricky. If they had programmed their software better it wouldn't be!
BlueJay wants you to provide a folder name from your project files to live in. However, BlueJay wants to create this folder. If you give it a folder that already exists, it will throw an error.
I created a folder called ProgammingMeritBadge, went into it, and then in the Folder name: bar I added the String "JavaExample" to the end of the path. Now BlueJay will create a project called JavaExample inside of my ProgrammingMeritBadge folder.
BlueJay wants you to provide a folder name from your project files to live in. However, BlueJay wants to create this folder. If you give it a folder that already exists, it will throw an error.
I created a folder called ProgammingMeritBadge, went into it, and then in the Folder name: bar I added the String "JavaExample" to the end of the path. Now BlueJay will create a project called JavaExample inside of my ProgrammingMeritBadge folder.
Now our BlueJay should look like this. The piece of paper image will take you to a readme.txt file if you double click it.
We don't need to complete a readme.txt file in order to write a Java program, so we'll just ignore this.
We don't need to complete a readme.txt file in order to write a Java program, so we'll just ignore this.
Java is an object oriented programming language which means all of the code is divided up into Classes. Each Class should contain all of the code for a single object. For example, if I were writing a watch program I would have a class for the time keeping, a class for an alarm setting, a class for the display, etc.
You can think of Classes as the explanation for how something works or exists, and objects or instances are specific examples. So I could tell you how a watch display works in general (the Class), and I could show you my watch display that I'm currently wearing (the object or instance).
In our example, we're going to create a Class called Field. We will use this class to create Field objects.
Click New Class...
Enter the Class Name of Field
Make sure the Class Type is Class
Click Ok
You can think of Classes as the explanation for how something works or exists, and objects or instances are specific examples. So I could tell you how a watch display works in general (the Class), and I could show you my watch display that I'm currently wearing (the object or instance).
In our example, we're going to create a Class called Field. We will use this class to create Field objects.
Click New Class...
Enter the Class Name of Field
Make sure the Class Type is Class
Click Ok
Now we should see the Field icon in the BlueJay project. Double click the Field icon to open the code editor panel.
BlueJay created our class with some automatically generated code. We don't need that!
Let's delete most of it and write our own!
Let's delete most of it and write our own!
Alright, let's start with this much code.
At the top in blue is a comment. This is where we can write things that will help other programmers who must update and maintain our software in the future. The program will ignore comments when it is compiled and run.
public class Field
This line tells Java what class this is. It is also called the class declaration.
At the top in blue is a comment. This is where we can write things that will help other programmers who must update and maintain our software in the future. The program will ignore comments when it is compiled and run.
public class Field
This line tells Java what class this is. It is also called the class declaration.
Our Field class needs attributes. When you think about a field, what attributes or properties come to mind? Size, name, type of turf, who plays there? There could be a number of things. For this example will stick with three: length, width, and a name.
We know that length and width should be numbers, but what kind of number? Whole or integer numbers? Decimal numbers? For this example we'll accept only whole numbers, no decimals. In Java whole numbers are called Integers and are declared using 'int'.
The name of the field should be text. In programming, text is generally referred to as strings. In Java it is a Class literally called String. It's a strange name, but think "string of characters" and it makes more sense.
The word 'private' refers to what other code can directly use these attributes. Things that are private can only be used by this Class. Things that are public can be used by any Class.
Note: Most lines in Java end with ';'. These lines are called statements, and if you don't end it with ';' then it will throw an error when you try to compile.
We know that length and width should be numbers, but what kind of number? Whole or integer numbers? Decimal numbers? For this example we'll accept only whole numbers, no decimals. In Java whole numbers are called Integers and are declared using 'int'.
The name of the field should be text. In programming, text is generally referred to as strings. In Java it is a Class literally called String. It's a strange name, but think "string of characters" and it makes more sense.
The word 'private' refers to what other code can directly use these attributes. Things that are private can only be used by this Class. Things that are public can be used by any Class.
Note: Most lines in Java end with ';'. These lines are called statements, and if you don't end it with ';' then it will throw an error when you try to compile.
Now we need a Constructor. This is the method that will be called every time we want to create a new Field object.
When we create the new Field object, we need to set the three attributes. So there needs to be three inputs to the Field constructor: two ints and one String.
length = l;
This is called an assignment statement. We take the value of the item on the right and give it or assign it to the value of the item on the left. In this case, the value for l that we input to the Constructor is being used to set our Field object's length attribute. The same is true for width and String.
When we create the new Field object, we need to set the three attributes. So there needs to be three inputs to the Field constructor: two ints and one String.
length = l;
This is called an assignment statement. We take the value of the item on the right and give it or assign it to the value of the item on the left. In this case, the value for l that we input to the Constructor is being used to set our Field object's length attribute. The same is true for width and String.
Creating a Field object is great, but what does it do? Right now nothing. Let's fix that.
A method most Java Classes have is called the toString method. This method is used to format the text that will be displayed if this object is every printed out to the command line (more on that later). In general you can name methods whatever you like, but if you name a method toString then this is a special case where Java will always call this method when it is printed.
public String toString()
This is the method declaration. It says the following:
1) This method is public and can be used by any other Classes.
2) This method will return a String object. If we don't need the method to return anything, we write 'void'.
3) This method is called toString and it has no inputs.
return "This field is called " + name + " .....
This line is the String that will be returned for this object. Notice how we use the '+' to combine text with the Field object's attributes. Java is smart enough to combine these for us as long as we use '+'.
You can see we also added the line 'import java.util.Scanner' to the very top of the file (outside of the class declaration). The Scanner Class was written by other Java programmers and it will help us when we request input from the user in the next panel.
A method most Java Classes have is called the toString method. This method is used to format the text that will be displayed if this object is every printed out to the command line (more on that later). In general you can name methods whatever you like, but if you name a method toString then this is a special case where Java will always call this method when it is printed.
public String toString()
This is the method declaration. It says the following:
1) This method is public and can be used by any other Classes.
2) This method will return a String object. If we don't need the method to return anything, we write 'void'.
3) This method is called toString and it has no inputs.
return "This field is called " + name + " .....
This line is the String that will be returned for this object. Notice how we use the '+' to combine text with the Field object's attributes. Java is smart enough to combine these for us as long as we use '+'.
You can see we also added the line 'import java.util.Scanner' to the very top of the file (outside of the class declaration). The Scanner Class was written by other Java programmers and it will help us when we request input from the user in the next panel.
So far we're written code to create Field objects and to nicely print them out, but we don't have any input or output code yet.
Let's create the method createField. As you can see, this method can be called by any other class, returns nothing, and has no inputs. The word 'static' is added so that it can be called by BlueJay.
First we need to understand how to get inputs from the user. In Java we will do this by using the Scanner class. The first line of this method creates the a new Scanner object that we'll call 'in'. To create a new Scanner object, the Scanner Class needs to know where to scan. In this case we will read from the command line, which in Java is called 'System.in'. We'll use this class in a minute.
System.out.println("Let's create a field!");
Next to need to output something to user to let them know what information we need from them. Outputting text is simple (although long) in Java. The Java object 'System.out' has a method 'println' we will use to output text. System.out (same with System.in) is a special Java object that does not need to be created before it can be used. So we'll use it to output a welcome message and then request some input.
int l = Integer.parseInt(in.nextLine());
This looks like a complicated statement, but let's break it down. We see that it's an assignment statement, so the stuff on the right will be given to a new int we're calling l. The call 'in.nextLine()' makes Java wait until the user has written something to the command line and then hit enter. This value will be returned as a String. A String is not an int, so we have to convert the String to an int. Java has another class called Integer (which does not need to be imported) that has a method 'parseInt'. This method converts a String to and int, so it is exactly what we want!
Then we ask for another input, this time for a width. And then the same way we created and assigned the l integer, we create and assign a w integer.
Finally, we need to know the name of the field. So we ask the user for this information. Then create and a assign String n the value of in.nextLine(). We learned before that in.nextLine() returns a String, and we need a String, so we don't have to do any conversion.
Field myField = new Field(l, w, n).
Finally, we create a Field object. Notice how we make sure that the order of our inputs exactly matches those of our Field Constructor.
System.out.println(myField);
Lastly, we ask Java to output our Field object. By writing the output statement this way, Java will automatically call Field's toString method. Although we could also write the code this way: 'System.out.println(myField.toString());'
Let's create the method createField. As you can see, this method can be called by any other class, returns nothing, and has no inputs. The word 'static' is added so that it can be called by BlueJay.
First we need to understand how to get inputs from the user. In Java we will do this by using the Scanner class. The first line of this method creates the a new Scanner object that we'll call 'in'. To create a new Scanner object, the Scanner Class needs to know where to scan. In this case we will read from the command line, which in Java is called 'System.in'. We'll use this class in a minute.
System.out.println("Let's create a field!");
Next to need to output something to user to let them know what information we need from them. Outputting text is simple (although long) in Java. The Java object 'System.out' has a method 'println' we will use to output text. System.out (same with System.in) is a special Java object that does not need to be created before it can be used. So we'll use it to output a welcome message and then request some input.
int l = Integer.parseInt(in.nextLine());
This looks like a complicated statement, but let's break it down. We see that it's an assignment statement, so the stuff on the right will be given to a new int we're calling l. The call 'in.nextLine()' makes Java wait until the user has written something to the command line and then hit enter. This value will be returned as a String. A String is not an int, so we have to convert the String to an int. Java has another class called Integer (which does not need to be imported) that has a method 'parseInt'. This method converts a String to and int, so it is exactly what we want!
Then we ask for another input, this time for a width. And then the same way we created and assigned the l integer, we create and assign a w integer.
Finally, we need to know the name of the field. So we ask the user for this information. Then create and a assign String n the value of in.nextLine(). We learned before that in.nextLine() returns a String, and we need a String, so we don't have to do any conversion.
Field myField = new Field(l, w, n).
Finally, we create a Field object. Notice how we make sure that the order of our inputs exactly matches those of our Field Constructor.
System.out.println(myField);
Lastly, we ask Java to output our Field object. By writing the output statement this way, Java will automatically call Field's toString method. Although we could also write the code this way: 'System.out.println(myField.toString());'
Alright, now let's test our code!
To do this, go back to the project panel and right click the Field icon. Then left click the 'void createField()' method.
To do this, go back to the project panel and right click the Field icon. Then left click the 'void createField()' method.
This will open a new panel called the Terminal. The Terminal or Command Line is where we have programmed all of our input and output to occur. So this is where we need to be.
As you can see, our first two outputs have already appeared and Java is now waiting for us to input a length.
Note: If you input something that is not a whole number, the program will crash. We have not taken the time to implement input detection, instead we have assumed the input will be an integer. This works for our simple test program, but just think if websites crashed if you typed in the wrong input!
As you can see, our first two outputs have already appeared and Java is now waiting for us to input a length.
Note: If you input something that is not a whole number, the program will crash. We have not taken the time to implement input detection, instead we have assumed the input will be an integer. This works for our simple test program, but just think if websites crashed if you typed in the wrong input!
Now I've run through the whole program, given three inputs, and we can see the result of our toString output.
Congratulations!
Congratulations!
Let's add some more to this program. (There might be a misspelling here. We programmers are good at coding, not at English. Please forgive).
Let's write a new method called isSquare. Again we'll make it public and have no input, but we'll have this method return a boolean. A boolean is a type of variable that can only be true or false. You don't need to create any boolean objects, Java will accept the words 'true' and 'false'.
We want the isSquare method to return true only when the field is a square, when length equals width. To do this we need to write an 'if-else' statement block.
if (length == width) {
This is the 'if' part of an 'if-else' statement block. If the value in between ( and ) is true, then the code in the 'if' block will be executed. Else the 'else' block code will be executed. In this case we are comparing length to width. The double equals ('==') is different from the single equals ('=') of the assignment statement. The double equals is checking for equality.
return true;
}
When the 'if' statement is true, this code will be executed and true will be returned by this method.
else {
return false;
}
If the comparison in the 'if' statement is false then this code will be executed. So false will be returned.
Now let's make use of this new method.
if (myField.isSquare()) {
System.out.println("This field is square");
}
Here is another 'if' statement, notice how we don't have an 'else' block to follow this. We don't need an 'else' block, if the 'if' statement isn't true, then nothing else gets executed. For this 'if' statement we are evaluating the result of myField.isSquare(). Remember how it returns a boolean meaning only true or false? This is exactly what an 'if' statement needs. So if the field is square, then we will print out a message saying this. Otherwise nothing extra will be printed.
Let's write a new method called isSquare. Again we'll make it public and have no input, but we'll have this method return a boolean. A boolean is a type of variable that can only be true or false. You don't need to create any boolean objects, Java will accept the words 'true' and 'false'.
We want the isSquare method to return true only when the field is a square, when length equals width. To do this we need to write an 'if-else' statement block.
if (length == width) {
This is the 'if' part of an 'if-else' statement block. If the value in between ( and ) is true, then the code in the 'if' block will be executed. Else the 'else' block code will be executed. In this case we are comparing length to width. The double equals ('==') is different from the single equals ('=') of the assignment statement. The double equals is checking for equality.
return true;
}
When the 'if' statement is true, this code will be executed and true will be returned by this method.
else {
return false;
}
If the comparison in the 'if' statement is false then this code will be executed. So false will be returned.
Now let's make use of this new method.
if (myField.isSquare()) {
System.out.println("This field is square");
}
Here is another 'if' statement, notice how we don't have an 'else' block to follow this. We don't need an 'else' block, if the 'if' statement isn't true, then nothing else gets executed. For this 'if' statement we are evaluating the result of myField.isSquare(). Remember how it returns a boolean meaning only true or false? This is exactly what an 'if' statement needs. So if the field is square, then we will print out a message saying this. Otherwise nothing extra will be printed.
Here I run the code as we did before, but this time I make sure to input the dimensions for a square field to see if our new code works.
It does, yay!
It does, yay!
Let's add another method. It will be similar to the last one. Let's call it isDoubleWide().
This time in the 'if-else' block, we'll use greater than ('>') rather than an equals ('==') comparison.
if (width > length*2) {
return true;
}
This will return true only when the width is more than twice the length. Notice how we can perform math calculations inside the comparison. We could also have written it this way:
int doubleWidth = length*2;
if (width > doubleWidth) {
return true;
}
But I like the first way I wrote it because it's less lines of code. Less code is generally better code.
I also added an extra 'if' statement in the createField method to check if this field is considered 'double wide'. This is nearly identical to the previous 'if' statement.
This time in the 'if-else' block, we'll use greater than ('>') rather than an equals ('==') comparison.
if (width > length*2) {
return true;
}
This will return true only when the width is more than twice the length. Notice how we can perform math calculations inside the comparison. We could also have written it this way:
int doubleWidth = length*2;
if (width > doubleWidth) {
return true;
}
But I like the first way I wrote it because it's less lines of code. Less code is generally better code.
I also added an extra 'if' statement in the createField method to check if this field is considered 'double wide'. This is nearly identical to the previous 'if' statement.
Finally we test to make sure our new code works that way we expect. It would be best if you run this test and then at least one more using values that should result with no double wide statement. This should be done for the square code as well.
It is always best to test all cases after you write new code. Your new code might have broken functionality that was previously working.
It is always best to test all cases after you write new code. Your new code might have broken functionality that was previously working.