Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a new project called LabMartiansIt has 2 files: Martian.javaMartianApp.ja

ID: 3820576 • Letter: C

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 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)We’ll 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:

namecount
e.g. C45

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.

Explanation / Answer


import java.util.Random;

/**

*
* @author Sam
*/
public class MartianApp {
    public static void main(String[] args) {
        Martian[] martians = initMartians();
        Martian[] namedInvaders = Martian.invade(13);
        for (Martian m:namedInvaders)
            System.out.println(m);
      
     
        Martian[] unknownInvaders = Martian.invade(13);
        for (Martian m:unknownInvaders)
            System.out.println(m);
    }

    private static Martian[] initMartians() {
        Martian[] m = new Martian[3];
        m[0] = new Martian();
        m[1] = new Martian();
        m[2] = new Martian();
        return m;
    }
  
}

class Martian {
    static int count = 0;
    String name;
    Random randof = new Random(new Random().nextLong());

    public Martian() {
        count ++;
        name = getRandomName();
    }
  
    void doSomthing() {
        if (Martian.count > 3)
            System.out.println("ATTACK");
        else
            System.out.println("HIDING");
    }
  
    private String getRandomName() {
        String s = ""+('a'+randof.nextInt()%26);
        s = s + randof.nextInt()%10 + randof.nextInt()%10;
        return s;
    }
  
    static Martian[] invade(int n){
        Martian[] m = new Martian[n];
        for (int i =0; i < n; i++)
            m[i] = new Martian();
        return m;
    }
  
    static Martian[] invade(){
        int n = new Random().nextInt()%9 + 2;
        Martian[] m = new Martian[n];
        for (int i =0; i < n; i++)
            m[i] = new Martian();
        return m;
    }

    @Override
    public String toString() {
        return name + "\" + count;
    }
  
}

The code is actually very simple. In case you are still facing problems understanding the code, please let me know. I shall try my best to help you with the code.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote