JAVA 1. Inherit a class Create a class called Overtime that inherits from Sports
ID: 3837635 • Letter: J
Question
JAVA
1.
Inherit a class
Create a class called Overtime that inherits from SportsGame. Add the following fields (Boolean suddenDeath, int minutes). Override the toString, and constructors. Create the setters and getters. Instantiate and call in a main.
2.
Exceptions and loop
At the console, create a program that asks the user for a number between 1 and 100. The routing should be idiot proof. It should catch ay exceptions and continue to ask until the user correctly enters the a valid number.
3.
Read and parse the books.xml file. Display in an attractive format the author, title, genre, and price. The books file was borrowed from https://msdn.microsoft.com/en-us/library/ms762271(v=vs.85).aspx.
Explanation / Answer
//Java code for question 1
import java.io.*;
import java.util.*;
interface SportGame
{
public String toString();
}
public class Overtime implements SportGame
{
int minute;
boolean suddenDeath;
public Overtime()
{
minute=0;
suddenDeath=false;
}
//Getter methods
public int getMinute()
{
return minute;
}
public boolean getSuddenDeath()
{
return suddenDeath;
}
//Setter methods
public void setMinute(int m)
{
minute=m;
}
public void setSuddenDeath(boolean t)
{
suddenDeath=t;
}
//Overriding toString method
public String toString()
{
return minute+" "+suddenDeath;
}
public static void main(String args[])
{
//Instantiating Overtime class and calling its methods and displaying results
Overtime o=new Overtime();
o.setMinute(10);
o.setSuddenDeath(true);
System.out.println(o.toString());
}
}
Output :
10 true
//Java code for 2nd question
import java.io.*;
import java.util.*;
public class Valid
{
public static void main(String args[])
{
int t;
Scanner s=new Scanner(System.in);
while(true)
{
try
{
System.out.println("Enter the number between 1 and 100 :");
t=s.nextInt();
if(t<1 || t>100)
throw new Exception();
else break;
}
catch(Exception e)
{
System.out.println("Invalid input -Please enter valid input");
}
}
}
}
//Output :
Enter the number between 1 and 100 :
124321
Invalid input -Please enter valid input
Enter the number between 1 and 100 :
3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.