0% found this document useful (0 votes)
3 views

Project 2 Java Main

The document contains an Android Java class 'MainActivity' that initializes two subclasses, 'Fighter' and 'Bomber', which inherit from an abstract class 'AlienShip'. It includes methods for firing weapons, detecting hits, and displaying the shield strength of the fighter and bomber ships through toast messages. The code demonstrates object-oriented programming concepts such as inheritance and method overriding in the context of a simple game-like application.

Uploaded by

A A Mamun
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Project 2 Java Main

The document contains an Android Java class 'MainActivity' that initializes two subclasses, 'Fighter' and 'Bomber', which inherit from an abstract class 'AlienShip'. It includes methods for firing weapons, detecting hits, and displaying the shield strength of the fighter and bomber ships through toast messages. The code demonstrates object-oriented programming concepts such as inheritance and method overriding in the context of a simple game-like application.

Uploaded by

A A Mamun
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.project22_8_21.

app;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Fighter aFighter = new Fighter();


Bomber aBomber = new Bomber();

//Can't do this AlienShip is abstract -


//Literally speaking as well as in code
//AlienShip alienShip = new AlienShip(500);

//But our objects of the subclasses can still do


//everything the AlienShip is meant to do
aBomber.shipName = "F-22 Raptor";
aFighter.shipName = "F-16 Falcon";

//And because of the overiden constructor


//That still calls the super constructor
//They have unique properties
Log.i("aFighter Shield:", "" + aFighter.getShieldStrength());
Log.i("aBomber Shield:", "" + aBomber.getShieldStrength());

//As well as certain things in certain ways


//That are unique to the subclass
aBomber.fireWeapon();
aFighter.fireWeapon();

//Take down those alien ships


//Focus on the bomber it has a weaker shield

aBomber.hitDetected();
aFighter.hitDetected();
aBomber.getNumShips();
aFighter.getNumShips();
}
Fighter aFighter= new Fighter();
Bomber aBomber= new Bomber();

public void fire_bomb(View v){


if(aFighter.getShieldStrength()>0) {
aFighter.hitDetected();
Toast.makeText(this, " Figher Get Damaged ",
Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(this, " Figher is allready dead ",
Toast.LENGTH_SHORT).show();
}
public void fire_fight(View v){
if(aBomber.getShieldStrength()>0) {
aBomber.hitDetected();
Toast.makeText(this, " Bomber Get Damaged ",
Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(this, " Bomber is allready dead ",
Toast.LENGTH_SHORT).show();

}
public void bomber(View v){
aBomber.getShieldStrength();
// aBomber.shipName = "F-22 Raptor";
Toast.makeText(this,"Bomber=
"+aBomber.getShieldStrength(),Toast.LENGTH_SHORT).show();
if(aBomber.getShieldStrength()==0)
{ Toast.makeText(this," Bomber has been
terminated",Toast.LENGTH_SHORT).show(); }
}
public void fighter(View v){
aFighter.getShieldStrength();
// aFighter.shipName="F-16 Falcon";
Toast.makeText(this,"Fighter Health=
"+aFighter.getShieldStrength(),Toast.LENGTH_SHORT).show();
if(aFighter.getShieldStrength()==0)
{ Toast.makeText(this,"Fighter has been
terminated",Toast.LENGTH_SHORT).show();}
}

You might also like