1. Conversion : Write a program that would allow the user to enter at least 5 nu
ID: 3710479 • Letter: 1
Question
1. Conversion: Write a program that would allow the user to enter at least 5 numbers. However, they must initially be entered in as strings and converted to int or double of your choosing and you must use try and catch and makes sure that when numbers are not entered, the exception is appropriately handled.
2. Receipt: Write a Java Class with two methods.
--- Public void writeFile (String fname); This method takes file name as input, creates a file with that name and allows user to enter double values in each line of the file.
--- Public double readFile(String fname); This method accepts file name as input, reads data from file and takes sum of all the values and returns it.
Create a Tester Class which tests this method and handle appropriate exceptions as discussed in class.
3. Inheritance: Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary. Write the class declarations, the constructors, and the methods toString for all classes. Supply a test program that tests these classes and methods.
4. Overriding: Write one class that has an addition method that add two numbers and the other class having the same addition method (same name) but adds three numbers. Call on these methods in the main method.
Explanation / Answer
1)
ReadIntOrDouble.java
import java.util.Scanner;
public class ReadIntOrDouble {
public static void main(String[] args) {
//Declaring variables
String s;
double nos[]=new double[5];
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the numbers entered by the user and populate those values into an array
for(int i=0;i<nos.length;)
{
System.out.print("Enter Number#"+(i+1)+":");
s=sc.next();
try
{
nos[i]=Double.parseDouble(s);
i++;
}
catch(NumberFormatException NFE)
{
System.out.println("** Invalid.Must be a number **");
}
}
//Displaying the Array elements
System.out.println(" Displaying the Array Elements :");
for(int i=0;i<nos.length;i++)
{
System.out.print(nos[i]+" ");
}
}
}
_________________
Output:
Enter Number#1:56.6
Enter Number#2:78.87
Enter Number#3:Hello
** Invalid.Must be a number **
Enter Number#3:54.5
Enter Number#4:bye
** Invalid.Must be a number **
Enter Number#4:32.4
Enter Number#5:88.8
Displaying the Array Elements :
56.6 78.87 54.5 32.4 88.8
_______________
2)
Receipt.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Receipt {
//This function will read the filename entered by the user
public void writeFile (String fname)
{
double number;
int i=0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
FileWriter fw=null;
//Getting the numbers entered by the user
try {
fw=new FileWriter(new File(fname));
System.out.print("Enter Number#"+(i+1)+"( 0 to Exit ):");
number=sc.nextDouble();
while(number!=0)
{
//Writing the numbers to the output file
fw.write(number+" ");
i++;
System.out.print("Enter Number#"+(i+1)+"( 0 to Exit ):");
number=sc.nextDouble();
}
//closing the output file
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//This function will open the file and read the numbers and calculate the sum
public double readFile(String fname)
{
double sum=0.0;
try {
Scanner s=new Scanner(new File(fname));
while(s.hasNext())
{
sum+=s.nextDouble();
}
s.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return sum;
}
}
___________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Receipt r=new Receipt();
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the filename entered by the user
System.out.print("Enter Filename :");
String fname=sc.next();
//calling the methods
r.writeFile(fname);
double sum=r.readFile(fname);
//Displaying the output
System.out.println("Sum of Nos :"+sum);
}
}
___________________
Output:
Enter Filename :numbers.txt
Enter Number#1( 0 to Exit ):56.6
Enter Number#2( 0 to Exit ):67.7
Enter Number#3( 0 to Exit ):87.7
Enter Number#4( 0 to Exit ):76.6
Enter Number#5( 0 to Exit ):65.5
Enter Number#6( 0 to Exit ):54.4
Enter Number#7( 0 to Exit ):43.3
Enter Number#8( 0 to Exit ):66.66
Enter Number#9( 0 to Exit ):0
Sum of Nos :518.46
____________
Outputfile:
numbers.txt
56.6
67.7
87.7
76.6
65.5
54.4
43.3
66.66
__________
__________________
Person.java
package org.students;
public class Person {
//Declaring instance variables
private String name;
private int birthYear;
//Parameterized constructor
public Person(String name, int birthYear) {
this.name = name;
this.birthYear = birthYear;
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBirthYear() {
return birthYear;
}
public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Name=" + name + ", BirthYear=" + birthYear;
}
}
_________________
Instructor.java
public class Instructor extends Person {
//Declaring instance variables
private double salary;
//Parameterized constructor
public Instructor(String name, int birthYear, double salary) {
super(name, birthYear);
this.salary = salary;
}
//getters and setters
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Instructor "+super.toString()+" Salary=" + salary;
}
}
_________________
Student.java
public class Student extends Person {
//Declaring instance variables
private String major;
//Parameterized constructor
public Student(String name, int birthYear, String major) {
super(name, birthYear);
this.major = major;
}
//getters and setters
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Student "+super.toString()+" Major=" + major;
}
}
__________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating an Instance of Student class
Student s=new Student("Kane Williams",1990,"Computer Science");
System.out.println(s);
//Creating an Instance of Instructor class
Instructor i=new Instructor("James Roy",1992,50000.00);
System.out.println(i);
}
}
___________________
Output:
Student Name=Kane Williams, BirthYear=1990 Major=Computer Science
Instructor Name=James Roy, BirthYear=1992 Salary=50000.0
____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.