Question
Create a new project called LabMartiansIt has 2 files: Martian.javaMartianApp.java Lab Martian Part-1 Class Martian Create a public class Martian with the followingfields: name of
Create a new project called LabMartiansIt has 2 files:
Martian.javaMartianApp.java
Lab Martian Part-1
Class Martian
Create a public class Martian with the followingfields:
name of type String count of type int this is a static variable randof type Random
initialize it right away
Create a parameter-less constructor Initialize the name by calling the methodgetRandomName (no parameters)Well write this method in a moment Increase the count by one
Create a method called getRandomName.It has no parameter and returns a name ofthe form:uppercase Letter + 2 digit number (10 99)e.g. A34, L82, W13
Override toStringIt should return the name in this form:
name\count e.g. C45\3
Class MartiansApp
Declare a public class MartianAppthat includes the main method
Create a martian and print it
Class Martian
Create a method calleddoSomething.It has no parameter and returnsnothing.
Inside the body of doSomething: If less than 5 martians have beencreated print name is hidinge.g. C34 is hidingotherwise print ATTACK!
Class MartiansApp
Create an array of Martian called
martians Use an array initializer to initializeit with 3 martians
The time to act has come. Use a loop to have all martians dosomething
Class Martian
Back in class Martian add a methodcalled invade. It is static, has oneparameter (the number of martiansthat should be created), and itreturns an array of martians
The method invade creates an arraythat includes the specified number ofMartians and returns them.
Class MartianApp
Extract a method that includes allthe code we have written so far inmain. Call that method test
In main do the following:Call invade and pass the value 13as an argument. Save the invadingMartians in a variable namedinvaders.
Use a loop to print the name ofeach of the invaders
Class Martian
Back in class Martian add anoverloaded method called invade.Again it is declared static and it returnsand array of martians. However, thistime it has no parameters.
The method invade creates a randomnumber of Martians (2 10) andreturns them.The actual number of Martiansreturned varies between method calls.
Class MartianApp
In main do the following:Call invade and pass no arguments.Save the invading Martians in avariable named unknownInvaders.
Use a foreach loop to loop throughall the unknownInvaders.Make each of them do something.
Run your program multiple times untilyou have seen the unknownInvadersboth hide and attack.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started