Design a console application that will print the fine due for a citizen who was caught speeding in a 120km zone. Make use of an abstract class named Fine that contains variables to store the citizen name, speed and fine payable. Create a constructor that accepts the citizen name and speed as parameters and create get methods for the variables. The Fine class must also contain a method to calculate the fine. If the speed is greater than or equal to 120km, multiply the speed by R10.20, or else no fine if payable.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
String name;
int speed;
System.out.printf("Enter name :");
name=in.nextLine();
System.out.printf("Enter speed :");
speed=Integer.parseInt(in.nextLine());
speeding_fines f=new speeding_fines(name, speed);
f.print();
}
}
abstract class Fine
{
private String name;
private int speed;
private double fine;
public Fine(String n, int s)
{
name = n;
speed = s;
setFine(speed);
}
private void setFine(int s)
{
if(s>120)
this.fine = s*10.20;
else
this.fine=0;
}
public String getName()
{
return name;
}
public void setName(String n)
{
name = n;
}
public int getSpeed()
{
return speed;
}
public void setSpeed(int s)
{
speed=s;
}
public double getFine()
{
return fine;
}
}
interface iFine
{
public void print();
}
class speeding_fines extends Fine
implements iFine
{
public speeding_fines(String name, int speed)
{
super(name, speed);
}
public void print()
{
System.out.println("Name: "+getName());
System.out.println("Speed: "+getSpeed());
System.out.println("Total fine: "+getFine());
}
}
Comments
Leave a comment