Java programming course: 6.4 Sorting arrays

In the previous lesson we showed how to loop over an array. Previous lesson: 6.3 Looping over an arrayCourse contents  Sorting arrays The Arrays class (which exists in package java.util) contains a number of static utility methods to facilitate the sorting of arrays. Sorting primitive arrays If you need to iterate over an array ensuring that t...

Java programming course: 6.3 Looping over arrays

In the previous lesson we defined a class that utilises an array. Previous lesson: 6.3 Example class using an arrayCourse contents Looping over arrays Java allows four different mechanisms for looping over the elements of an array. Looping is sometimes referred to as iteration. The standard "for" loop The standard for loop is in the following forma...

Java programming course: 6.2 Example class using an array

In the previous lesson we looked at what arrays are. Previous lesson: 61. ArraysCourse contents  Example class using an array You will now develop a simple class that uses a single-dimensional array to model a pen that contains a group of animals. Create a new class in virtualzoo.core called Pen as follows that declares a constant and three in...

Java programming course: 6.1 Arrays, loops, and sorts

In the previous lesson we looked at arithmetic operations on primitives. Previous lesson: 5.11 Arithmetic operations on primitivesCourse contents  Arrays, loops, and sorts Arrays provide a simple means of grouping like objects that they can be processed as a whole. Loops enable you to repeat parts of your code a certain number of times, while ...

Java programming course: 5.11 Arithmetic operations on primitives

In the previous lesson you learnt about casting. Previous lesson: 5.10 CastingCourse contents Arithmetic operations on primitives Enter your text here ... The arithmetic operators are: + for addition: e.g., a + b- for subtraction: e.g., a - b* for multiplication: e.g., a * b/ for division: e.g., a / b% for modulus (i.e., the remainder after divisio...

Java programming course: 5.10 Casting

In the previous lesson you learnt about conditionals using the switch...case... block. Previous lesson: 5.9 Conditionals using switch caseCourse contents  Casting Enter your text here ... You previously learned about the different capacities of the numeric primitive types, i.e., byte (8 bits), short (16 bits), int (32 bits), long (64 bits), ch...

Java programming course: 5.9 Conditionals using switch case

In the previous lesson you leant about the ternary operator. Previous lesson: 5.8 The ternary operatorCourse contents Conditionals using switch case An alternative structure to if…else… is the switch…case… block. You can only use this when testing for equality and for the integer primitives (byte, short, int, long and char) and their wrapper classe...

Java programming course: 5.8 The ternary operator

In the previous lesson you learnt about the if...else... block. Previous lesson: 5.7 The if else blockCourse contents The ternary operator There is a short-cut technique you can sometimes use in place of simple if...else... blocks. Consider the following code: int temperature = 27;String weather = "?";if (temperature > 25) { weather = "hot";} el...

Java programming course: 5.7 The if else block

In the previous lesson you learnt about compound operators. Previous lesson: 5.6 Compound operatorsCourse contents  The if else block You can use the else statement when you need to specify what should happen if the condition is not met. For example, you could modify the weatherCheck() method from the previous lesson: public void weatherC...

Java programming course: 5.6 Compound operators

In the previous lesson you learnt about logical operators and the if condition: Previous lesson: 5.5 Logical operators and the if conditionCourse contents  Compound operators If you need to test more than one condition at the same time you can use the logical and operator && or the logical or operator ||: // Compound operatorsint i = 3...

Java programming course: 5.5 Logical operators and the if statement

In the previous lesson you learnt about using enum for constants. Previous lesson: 5.4 Using enum for constantsCourse contents  Logical operators and the if statement Frequently in programming you need to check whether a certain condition is true and perform different processing if the condition holds to when the condition does not hold. Java ...

Java programming course: 5.4 Using enum for constants

In the previous lesson you learnt about constants.  Previous lesson: 5.3 ConstantsCourse contents Using enum for constants You will commonly see the above technique of static final variables to define constants, including in the Java APIs. However, since Java version 5, a new and more powerful way of defining constants has been available, know...

Java programming course: 5.3 Constants

In the previous lesson we looked System.out.println(). Previous lesson: 5.2 Understanding System.out.printlnCourse contents Java Constants Another common use of class level (i.e., static) variables is for the definition of constants. These are values which once set, cannot be changed. In the Animal class there exists the instance variable gender wh...

Java programming course: 5.2 Understanding System.out.println

In the previous lesson we looked at static members. Previous lesson: 5.1 Static membersCourse contents Understanding System.out.println() You have several times used the System.out.println() statement to send text to the Output window, and its syntax looks slightly unusual in that there are two dots. If you look at the API for the System class, you...

Java programming course: 5.1 Static members, constants, and conditionals

In the previous lesson we started to manage the zoo using class ZooAdministrator. Previous lesson: 4.7 Manage zoo using ZooAdministratorCourse contents Static members, constants, and conditionals The static keyword enables you to define a variable or method that is not dependent upon any object. A constant is a variable whose value cannot be change...

Java programming course: 4.7 Managing the zoo using ZooAdminstrator

In the previous lesson you learnt about Java interfaces. Previous lesson: 4.6 InterfacesCourse contents  Managing the zoo using ZooAdminstrator From this point in the course, you will start to make more use of the ZooAdministrator class by using it to store example animals, zookeepers, visitors, etc. The VirtualZoo class, which is the entry po...

Java programming course: 4.6 Interfaces

In the previous lesson we wrote a class to model a zoo visitor. Previous lesson: 4.5 Class to model a zoo visitorCourse contents  Interfaces A Java interface (not to be confused with a user interface) is like a class in that it defines a named type that has a group of related method specifications. In an interface, however, there are no method...

Java programming course: 4.5 Class to model a zoo visitor

In the previous lesson you learnt about keywords this and super. Previous lesson: 4.4 Keywords this and superCourse contents Class to model a zoo visitor To model a zoo visitor let's assume you establish that only three pieces of information are required; their name, email address and the animal they are sponsoring, although they might not be spons...

Java programming course: 4.4 Keywords this and super

 In the previous lesson you wrote a class to model a zookeeper. Previous lesson: 4.3 Class to mode a zookeeperCourse contents Keywords this and super The keyword this is used to refer to the current instance, sometimes known as the receiver object. You have previously used it when overloading constructors in order to delegate to a different co...

Java programming course: 4.3 Class to model a zookeeper

In the previous lesson you learnt about the Object class: Previous lesson: 4.2 The Object classCourse contents Class to model a zookeeper The zoo will of course require a number of zookeepers to help look after the animals, so you will now develop a ZooKeeper class. Just as you did with the Animal class, you need to think about the attributes and m...