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

Project 17-2: Convert lengths Console Welcome to the Length Converter 1 - Conver

ID: 3735655 • Letter: P

Question

Project 17-2: Convert lengths

Console

Welcome to the Length Converter

1 - Convert a length

2 - Add a type of conversion

3 - Delete a type of conversion

4 - Exit

Enter menu number: 1

1 - Miles to Kilometers: 1.6093

2 - Kilometers to Miles: 0.6214

3 - Inches to Centimeters: 2.54

Enter conversion number: 2

Enter Kilometers: 10

10.0 Kilometers = 6.214 Miles

1 - Convert a length

2 - Add a type of conversion

3 - Delete a type of conversion

4 - Exit

Enter menu number: 2

Enter 'From' unit: Centimeters

Enter 'To' unit: Inches

Enter the conversion ratio: .3937

This entry has been saved.

1 - Convert a length

2 - Add a type of conversion

3 - Delete a type of conversion

4 - Exit

Enter menu number: 1

1 - Miles to Kilometers: 1.6093

2 - Kilometers to Miles: 0.6214

3 - Inches to Centimeters: 2.54

4 - Centimeters to Inches: 0.3937

Enter conversion number: 4

Enter Centimeters: 2.54

2.54 Centimeters = 1 Inches

1 - Convert a length

2 - Add a type of conversion

3 - Delete a type of conversion

4 - Exit

Enter menu number: 4

Goodbye.

Project 17-2: Convert lengths (cont.)

Operation

·        This application begins by displaying a main menu with four items.

·        If the user chooses the first main menu item, the application displays a menu of possible conversions. After the user selects a conversion, the application prompts the user to enter a unit of measurement, calculates the conversion, displays the result, and displays the main menu again.

·        If the user chooses the second main menu item, the application prompts the user to enter the values for a new conversion, saves this new conversion to a file, and displays a message to the user.

·        If the user chooses the third main menu item, the application displays a menu of possible conversions. After the user selects the conversion, the application deletes that conversion from the file, displays a message to the user, and displays the main menu again.

·        If the user chooses the fourth main menu item, the application displays a goodbye message and exits.

Specifications

·        Create a class named Conversion that can store information about a conversion, including the from unit, from value, to unit, to value, and conversion ratio. This class should also contain the methods that perform the conversion calculations and return the results as a formatted string.

·        Create a class named ConversionIO that contains two methods: one that reads an array list of Conversion objects from a file and another that writes an array list of Conversion objects to a file. For example:

public static ArrayList<Conversion> getConversions()

public static void saveConversions(ArrayList<Conversion> conversions)

·        Store the list of conversions in a text file named conversion_types.txt in the same directory as the ConversionIO class. If the conversion_types.txt file doesn’t exist, the ConversionIO class should create it. This class should use buffered I/O streams, and it should close all I/O streams when they’re no longer needed.

·        Create a class named ConversionsApp that displays the menus and responds to the user’s choices.

·        Use the Console class shown in chapter 8 or a variation of it to get the user’s entries.

Explanation / Answer

Solution:

code:

Converion.java

//package lengthconverter;

import java.text.DecimalFormat;

public class Conversion {

String fromUnit;

String toUnit;

double conversionRatio;

//Creates varibales to store conversion data

public Conversion(String fromUnit, String toUnit, double conversionRatio) {

super();

this.fromUnit = fromUnit;

this.toUnit = toUnit;

this.conversionRatio = conversionRatio;

}

//Displays type of conversion made

public String getType() {

return fromUnit + " To " + toUnit + ":" + conversionRatio;

}

//Calculates conversion and stores as

public String conversionCalc(double fromValue) {

double toValue = fromValue * conversionRatio;

DecimalFormat decFormat = new DecimalFormat("#.##");

toValue = Double.valueOf(decFormat.format(toValue));

return String.valueOf(toValue);

}

}

ConversionIO.java

import java.util.*;

import java.io.*;

class ConversionIO{

public static ArrayList<Conversion> getConversions(){

ArrayList<Conversion> temp = new ArrayList<Conversion>();

try{

Scanner in = new Scanner(new File("conversion_types.txt"));

String from, to;

double ratio;

while(in.hasNextLine()){

from = in.next();

to = in.next();

ratio = in.nextDouble();

temp.add(new Conversion(from, to, ratio));

}

}

catch(FileNotFoundException e){

System.out.println("conversion_types.txt not found");

}

return temp;

}

public static void saveConversions(ArrayList<Conversion> conversions){

try{

PrintWriter out = new PrintWriter("conversion_types.txt");

for(Conversion item: conversions){

out.println(item.fromUnit+" "+item.toUnit+" "+item.conversionRatio);

}

out.close();

}

catch(FileNotFoundException e){

System.out.println("Unable to create file");

}

}

}

ConverionsApp.java

import java.util.*;

public class ConversionsApp {

public static void main(String args[]) {

ArrayList<Conversion> list = ConversionIO.getConversions();

System.out.println("Welcome to the Length Converter: ");

int choice, i;

Scanner in = new Scanner(System.in);

do {

System.out.println(" 1 - Convert a length");

System.out.println("2 - Add a type of conversion");

System.out.println("3 - Delete a type of conversion");

System.out.println("4 - Exit");

System.out.println("Enter menu number: ");

choice = in.nextInt();

switch (choice) {

case 1:

i = 0;

for (Conversion item : list) {

System.out.println(i + 1 + " - " + item.getType());

i++;

}

int number;

System.out.println("Enter conversion number: ");

number = in.nextInt();

if (number <= list.size()) {

double inp;

System.out.print("Enter " + list.get(number - 1).fromUnit + ": ");

inp = in.nextDouble();

System.out.println(inp + " " + list.get(number - 1).fromUnit + " = "

+ list.get(number - 1).conversionCalc(inp) + " " + list.get(number - 1).toUnit);

} else {

System.out.println("Invalid Conversion");

}

break;

case 2:

String from, to;

double ratio;

System.out.print("Enter 'From' unit: ");

from = in.next();

System.out.print("Enter 'To' unit: ");

to = in.next();

System.out.print("Enter the conversion raio: ");

ratio = in.nextDouble();

list.add(new Conversion(from, to, ratio));

ConversionIO.saveConversions(list);

System.out.println("This entry has been saved");

break;

case 3:

i = 0;

for (Conversion item : list) {

System.out.println(i + 1 + " - " + item.getType());

i++;

}

int n;

System.out.println("Enter conversion number that you want to delete: ");

n = in.nextInt();

if (n <= list.size()) {

list.remove(n - 1);

ConversionIO.saveConversions(list);

System.out.println("Successfully conversion deleted");

} else {

System.out.println("Invalid Conversion");

}

break;

case 4:

System.out.println("Goodbye");

System.exit(1);

default:

System.out.println("Invalid choice");

}

} while (choice != '4');

}

}