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...

Java programming course: 4.2 The Object class

In the previous lesson we covered packages and imports. Previous lesson: 4.1 PackagesCourse contents  The Object class You saw earlier that any class can extend (i.e., inherit from) any other class, and this can be another class written by you or someone else, or a Java supplied class. In fact, every class always inherits from a special Java s...

Java programming course: 4.1 Packages, the Object class, and interfaces

In the previous lesson we tidied up the Animal constructors. Previous lesson 3.6: Tidying up the Animal constructorsCourse contents Packages, the Object class, and interfaces Packages allow you to partition your classes into sets of logical groups, and enables you to protect parts of your application from other parts. The Object class provides the ...

Java programming course: 3.6 Tidying up the Animal constructors

In the previous lesson we looked at abstract classes and methods. Previous lesson 3.5 Abstract classes and methodsCourse contents  Tidying up the Animal constructors Since making the Animal class abstract the only constructor that ever gets invoked within Animal is the one requiring three arguments, which is called by its subclasses using the ...

Java programming course: 3.5 Abstract classes and methods

In the previous lesson you learnt about overriding. Previous lesson: 3.4 OverridingCourse contents Abstract classes and methods Now that you have created three subclasses of Animal you might like to consider what is meant by "animal": does it ever make sense to instantiate an Animal object that isn't a specific type of animal? It seems natural to t...

Java programming course: 3.4 Overriding

 In the previous lesson you learnt about inheritance. Lesson 3.3 InheritanceCourse contents Overriding The zoo would like to know which animal types are endangered, even though most types in the zoo are not. Add the following method within the Animal class:   public boolean isEndangered() { return false;} Note that for getter methods that...

Java programming course: 3.3 Inheritance

In the previous lesson you learnt about overloading. Lesson 3.2 OverloadingCourse contents  Inheritance It was mentioned earlier that there is currently no obvious way of specifying what type of animal is being created. You might guess that Bruno could be a dog or that Tiddles could be a cat, but there is no guarantee of this. For the zoo appl...