Вы находитесь на странице: 1из 3

Assignment 1

John Pham | Computer Science BSc | K1630506

Introduction
• The objectives of this assignment, are to print a series of messages. This includes:
o A name
o The Word ‘Uniform’
o The number greater than the supplied number, which is 7.

• The assignment will require the use of two classes:


o MessagePrinter, which will act as a “driver” for the program. It will contain the main
method, and therefore, will be compiled using the command line. It will utilise the
methods from within other classes.
o Messages, contains no main method. It contains 3 methods, with each method printing
different content. The methods from within this class are called by the main function
from within the class MessagePrinter.

Pseudocode

Class MessagePrinter

Instantiate Class Messages into Object copyOfMessages

Go to Method namePrinter from Object copyOfMessages


Go to Method wordPrinter from Object copyOfMessages
Go to Method incrementNumber from Object copyOfMessages

Class Messages

Method namePrinter
Print “John Pham”

Method wordPrinter
Print “Uniform”

Method incrementPrinter
Print “7+1”

Class Diagram

Class
Class Messages
MessagePrinter
Methods
Methods namePrinter(): void
Main(String[]): void wordPrinter(): void
incrementPrinter():void
Line by Line description for the Class MessagePrinter

public class MessagePrinter{


This declares a new class called MessagePrinter. In here, contains nothing else but our main
method. It is the “Driver” of our program. This is the class we will need to compile using the
command line. It is important that the file name matches the name of this method.

public static void main(String[] args){


This the main method. This is the method that will be executed first, from within this method, we will call
on other methods which allow our program to function as it should.

Messages copyOfMessages = new Messages();


This declares a new variable called copyOfMessages, inside this variable, we have also declared that
the type of the variable is Messages, this is because the ‘type’, describes what sort of data the following
variable will contain. In this case, the type will be the name of the class of which we are copying. In order
to copy the class Messages into the variable copyOfMessages, we need to request this with the
notation new. Essentially, a copy of the class Messages will be put inside the variable
copyOfMessages, we call this variable an Object. It is important that all the class files required for this
program is placed within the same folder.

copyOfMessages.namePrinter();
This calls the method namePrinter, which can be found from within the object copyOfMessages

copyOfMessages.wordPrinter();
This calls the method wordPrinter, which can be found from within the object copyOfMessages

copyOfMessages.incrementPrinter();
This calls the method incrementPrinter, which can be found from within the object copyOfMessages
Line by Line description for the Class Messages

public class Messages{


This declares a new class called Messages. In here, contains 3 methods. The methods from within this
class are not initially executed until called on by the main method. We do not need to compile this class
from the command line.

public static void namePrinter(){


This declares a public method called namePrinter. This method does not return any value, so our
return type is void. There are no arguments to pass to this method, so there is nothing in-between the
brackets.

System.out.println("John Pham");
System.out.println is a method from the default java library which prints out some argument. In
this case, the literal “John Pham” is printed.

public static void wordPrinter(){


This declares a public method called wordPrinter. This method does not return any value, so our
return type is void. There are no arguments to pass to this method, so there is nothing in-between the
brackets.

System.out.println("Uniform");
The literal “Uniform” is printed.

public static void incrementPrinter(){


This declares a public method called incrementPrinter. This method does not return any value, so
our return type is void. There are no arguments to pass to this method, so there is nothing in-between
the brackets.

System.out.println(7+1);
The number 8 is printed, because we are asking Java to perform an arithmetic operation, using the
symbol ‘+’, on the numbers 7, and 1.

Вам также может понравиться