0% found this document useful (0 votes)
6 views6 pages

Lecturer Leila Lab Java

The document outlines a class exercise focused on Java programming, specifically on calculating the time between two dates using the LocalDate class. It includes instructions for creating a project in NetBeans, developing a 'Covid' class, and implementing methods for managing a virtual farm with cows, including behaviors like milking and feeding. The exercise emphasizes object-oriented programming principles and the use of collections to manage multiple instances of classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Lecturer Leila Lab Java

The document outlines a class exercise focused on Java programming, specifically on calculating the time between two dates using the LocalDate class. It includes instructions for creating a project in NetBeans, developing a 'Covid' class, and implementing methods for managing a virtual farm with cows, including behaviors like milking and feeding. The exercise emphasizes object-oriented programming principles and the use of collections to manage multiple instances of classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecturer: Leila Rovincanni

Room 346

Time: 9:00

First exercise

Database contains data, so to prepare you for dealing with this we will take this
exercise.

This class intend to help students to understand Java. The main goal here is to find
the period between the two things, how you going to get the input, insert day,
month, and year of your birthday. You use this data to calculate the timing
between the two dates.

Comments on the solution of the first activity:

Class used is local date, extend only one class, but implement number of interfaces.

The two methods we can use is (of) 3 integers

Go back to NetBeans and create a project, if you have (And) use it, I will use simple
java application

Do the usual labs and then lab2o2 and I will name it lab2.

I will go to the source packages; I will add a new class there.

Class name will be Covid.

Pay attention, it gives you the full path to how to get to this file.

I will create the main function.

I am going to hard code it.

We can just print it.

Looks like Maven is not set up, so I will use another framework.
To find the differences between the two, we can use this fastperiod and the method
used will be between and it will give us the time that laps between the two
dates.

Between method is static so it is available to everyone, so I do not need the name of


the object

We can print it out.

The way it is printed is day/month/ year

I can use methods such as getDays, getMonths, GetYears

Once you have that you can do what else is needed

Line 34 replaces all this, if you do not want to hard code you can use the methods

You can just call it here between methods.

Two local dates: the starting and the other date and create a object of class

If you see java code, it is normal to see things like this.

Java is a mature language, keep track of days and of times

It is only one way of doing it, any combination is also ok, if you want to use objects.

Brackets can be confusing, but this is the way of grouping elements

Last time we did the guessing, 2b, and 2c is very similar activity to 2b.

Sometimes they just made the website and cuts in pages

Recap events objects create a class, and it will be a module

Class animal (super class) and then sub-classes such as mammals, pets …

Here tree factors of the funny behaviour of a cow constructions

Session of other methods:

Milking, default package

Public is available to everyone


Modelling your object= moods and milking

GetMood – depends on the amount of millk

Copy the code provide, this class literally is just used for testing

When you go back to the handout so you can create a virtual farm and use a certain
number of cows:

3 anger cows

Finishing everything related to the cows itself,

Milk a hungry cow, she become very angry,

There is no Nap option

We start with the two string methods and again that goes to the cow class, right click
insert code, find the two strings, and add method

More appropriate printout.

It will not exist really angry string, so we can build the string ourselves and then if the
cow is really angry we can extend the result with additional information which is to
give string representation of the object, in this case we do not have, so it is just to
run and test it.

Let’s reduce the amount of milk, there is not a explicit method to define the amount
of the milk:

Default to create the amount of milk

We can call the method four times

We have the printout, but we have not really implemented the method, so we need to
change the month, now we have a printout to say that the cow is really angry

If you go back to the handout, check the information there to implement this
functionality you need to be inside the method.

If the cow is angry, it means the amount of milk is 0, you can either check the milk or
say …
Basic behaviour, but check milking method, it is where can have problems, when
someone try to milk the cow it can unease the other animals.

New class java, class named farm and it will be at the same package.

The farm itself is a collection of cow, you decide what to have in there an array list or

I will be more descriptive here; a list of objects and you can put them in a array list

Instances of one class will be stored in that array.

You can specify the inicial capacity and that will create an array list, then we can add
two string methods after and then we can go and create a farm: myFarm, I will print
because at the moment we do not have a method to enter a cow to this farm, as we
can see it is empty.

Let’s get a method that can store a cow and add it to the farm:

 A number of options when you using Add method.


 List method
 The position and local
 Add the cow to the farm and test it
 Mycow is a collection so when print it will print each element of the collection
 Static methods we do not to create an object
 One place to keep the class variable
 We need to keep a track of the number of angry cow
 Remember the number defined was three
 Variables which check what happens with the cows
 We can then modify the, initially 0, but when the cow become angry, we can
modify that

We can do this in the farm

Cow really angry we increase that, be careful, because a really angry cow can calm
down after being fed.

And if this number goes down, all happening in the cow class, we can implement this
on two ways

In the farm:
 Getting cow as index 1 and feeding it
 Generate a number and using that to initiate the activity
 Pick the cow in the farm always a different cow, in the feed method you will be
doing random generation

In the main method:

 If the number is even cow is feed


 If the number is odd cow is milk
 Until the number of cows is true

( Leila started to create the class meetings, you can go online and book a slot. Also
used as attendance, so if you are in, go online get the attachments and I will know
that you were in for the class.)

The program terminates when there are more than 3 angry cows, to terminate the
program you can use loop.

The method could be changed:

 Generate a random number and get a angry cow and feed it


 If you get 3 angry ones it terminates, it make sense to have more than one
cow in the farm
 An object of the farm and do the action to the object
 More precise overload using equals
 Abstract method because is a method without the body
 When you use the options from NetBeans it gives you a signature and u use
this
 Comparable interface and get information, just compare 1,2 method,
implement it, comparing two instances of this class and you decide how you
would like to compare them, comparing their names, or amount of milk, it
depends on what you want to compare.
 The easiest way is to check if they are equal and if they are it will just return 0.
 Comparing is always to return an integer number which could be 0 or could be
something else
 Return 0 if they are identical.
 Do the casting as given here, what it does is to handle as a cow, as an object
of our class, we are checking if this object is an element of our class.

Comparing the names of the cows in this method:

 If it is equal to 0 than you can give two cows with same name, compare the
among of milk.
 Comparing methods.
 Comparable objects.

You might also like