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

java Write a program using console dialog (see InputTest.java and Retirement.jav

ID: 662783 • Letter: J

Question

java

Write a program using console dialog (see InputTest.java and Retirement.java) to read two or more resistance values and calculate the total resistance if the resistors are in series and if the resistors are in parallel.

Modify the program to use command line arguments for the resistance values.

Modify the program again to use JOptionPane

Create a bin directory on your computer and add it to your path. Download the utility dsize.zip and unpack it into the bin directory. Open a command window, change (cd) to some folder that has sub-folders, and enter the command:

Capture a screen image of the resulting output and embed the image in your submission.

Download hilite.zip and get the source-highlighting program to work. Note: copy the un-zipped files in Src-Highlite.zip to your C:Program Files directory. Keep the same directory structure.

You should submit an index.html document summarizing your work and reporting validation tests. You should also submit source files and other files helpful in reconstructing or verifying your work.

Explanation / Answer

NOTE : Create 3 classes. 1. Main.java ; 2. Resistance.java ; 3. ClosingFrame.java. Copy each of the elow respective code in there respective files. Keep all the three Java files at same location.

For excercise 4. You need to install the file in your PC and run it. Pleas download and run the file as instructed.

For excercise 5. Youjust need to extract the files in your C drive>> Program Files.

1. & 2.

Main.java

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
/**
* Makes a Constant.
*/
public final static int DONE = 0;

/**
* Makes a Constant.
*/
public static final int USER_CHOICE_SERIES = 1;

/**
* Makes a Constant.
*/
public static final int USER_CHOICE_PARALLEL = 2;

public static void main(final String[] args) {

// TODO Auto-generated method stub
DecimalFormat f = new DecimalFormat("##.00");
Resistance myRes = new Resistance();
Scanner keyboard = new Scanner(System.in);

//Display Purpose
System.out.println(" This program will calculate "
+ "the resistance of a Parallel or Series "
+ "Circuit ");   

//Display instructions
System.out.println("Please enter the value of your "
+ "resistors separately, when done press 0 (zero)");
int n = 0;
//holds current resistor's value
Double val;

while(true) {
System.out.println("Enter Resistor #" + ++n);
val = keyboard.nextDouble();

//only add if higher than 0
if (val > 0.0) {
myRes.resistor.add(val);
}
//0 entered, so finish the loop
else break;
}
// Ask Which Method To Use,
do {
System.out.println(" Enter 1 to calculate "
+ "a Series Circuit "
+ "or 2 for a Parallel Circuit");
myRes.userChoice = keyboard.nextInt();

// Ask user again in case he enters something else
} while (myRes.userChoice != USER_CHOICE_PARALLEL
&& myRes.userChoice != USER_CHOICE_SERIES);

//Output the total resistance
System.out.printf(" The Total Resistance is "
+ f.format(myRes.calculateResistance()));
  
System.out.println(" Total Resistance in Series : " + myRes.calculateSeriesResistance());
System.out.println("Total Resistance in Parallel : "+myRes.calculaParallelResistance());
System.out.println("Total Combined Resistance is "+(myRes.calculaParallelResistance()+myRes.calculateSeriesResistance()));
  
}
}

Resistance.java

import java.util.ArrayList;

public class Resistance {

/**
* Holds an object type.
*/
public int userChoice;

ArrayList<Double> resistor = new ArrayList<Double>();

/**
*Chooses which process follows next.
*@return circuitType
*/
public final double calculateResistance() {
if (userChoice == 1) {
return calculateSeriesResistance();
} else {
return calculaParallelResistance();
}
}
/**Returns object of parallel circuit.
*
* @return 1 / runningResistance.
*/

public double calculaParallelResistance() {
double runningResistance = 0;

//use size() instead of length, which doesn't exist in ArrayList
for (int index = 0; index < resistor.size(); index++) {
//also, iterate over each resistor's value, and to get it,
//use...err, get() method :)
runningResistance += (1 / resistor.get(index));
}
return 1 / runningResistance;
}

/**Returns object of series circuit.
*
* @return runningResistance.
*/
public double calculateSeriesResistance() {
double runningResistance = 0;
for (int index = 0; index < resistor.size(); ++index) {
runningResistance = runningResistance + resistor.get(index);
}
return runningResistance;
}
}

3. jOptionPane

Class : ClosingFrame

// Imports
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.Scanner;

import javax.swing.*;

class ClosingFrame
       extends    JFrame
       implements   ActionListener
{
   // Instance attributes used in this example
   private   JPanel   topPanel;
   private   JButton   buttonError;
   private   JButton   buttonWarning;
   private   JButton   buttonInfo;
   private   JButton   buttonQuestion;
   private   JButton   buttonPlain;
  
   /**
* Makes a Constant.
*/
public final static int DONE = 0;

/**
* Makes a Constant.
*/
public static final int USER_CHOICE_SERIES = 1;

/**
* Makes a Constant.
*/
public static final int USER_CHOICE_PARALLEL = 2;

public static double series=0.0;
  
public static double parallel=0.0;


   // Constructor of main frame
   public ClosingFrame()
   {
       // Set the frame characteristics
       setTitle( "Dialog Test Frame" );
       setSize( 310, 130 );
       setBackground( Color.gray );

       // Create a panel to hold all other components
       topPanel = new JPanel();
       topPanel.setLayout( new FlowLayout() );
       getContentPane().add( topPanel );

       // Create a button for each message type
       buttonError = new JButton( "Series Resistance" );
       topPanel.add( buttonError );
       buttonInfo = new JButton( "Parallel Resistance" );
       topPanel.add( buttonInfo );
      

       // add an action listener to listener for button clicks
       buttonError.addActionListener( this );
       buttonInfo.addActionListener( this );
   }

   // ActionListener handler to listener for button clicks
   // within this application fram
   public void actionPerformed( ActionEvent event )
   {
       // Display a message on the console
       System.out.println( event );

       if( event.getSource() == buttonError )
       {
           JOptionPane dialog = new JOptionPane();
           dialog.showMessageDialog( this, series ,
                   "Series Resistance", JOptionPane.ERROR_MESSAGE );
       }
       else if( event.getSource() == buttonWarning )
       {
           Object[] possibleValues = { "First", "Second", "Third" };
           JOptionPane dialog = new JOptionPane();
           Object selectedValue = dialog.showInputDialog( this,
                   "This is a warning",
                   "Warning", JOptionPane.WARNING_MESSAGE,
                   null, possibleValues, possibleValues[0] );
       }
       else if( event.getSource() == buttonInfo )
       {
           JOptionPane dialog = new JOptionPane();
           dialog.showConfirmDialog( this,
                   parallel,
                   "Parallel Resistance", JOptionPane.CANCEL_OPTION,
                   JOptionPane.INFORMATION_MESSAGE, null );
       }
       else if( event.getSource() == buttonQuestion )
       {
           JOptionPane dialog = new JOptionPane();
           dialog.showConfirmDialog( this, "Is this a question?",
                   "Question", JOptionPane.YES_NO_OPTION,
                   JOptionPane.QUESTION_MESSAGE, null );
       }
       else if( event.getSource() == buttonPlain )
       {
           JOptionPane dialog = new JOptionPane();
           dialog.showConfirmDialog( this, "This is a plain message",
                   "Plain", JOptionPane.DEFAULT_OPTION,
                   JOptionPane.PLAIN_MESSAGE, null );
       }
   }

   // Main entry point for this example
   public static void main( String args[] )
   {
       // Create an instance of the test application
       DecimalFormat f = new DecimalFormat("##.00");
Resistance myRes = new Resistance();
Scanner keyboard = new Scanner(System.in);

//Display Purpose
System.out.println(" This program will calculate "
+ "the resistance of a Parallel or Series "
+ "Circuit ");   

//Display instructions
System.out.println("Please enter the value of your "
+ "resistors separately, when done press 0 (zero)");
int n = 0;
//holds current resistor's value
Double val;

while(true) {
System.out.println("Enter Resistor #" + ++n);
val = keyboard.nextDouble();

//only add if higher than 0
if (val > 0.0) {
myRes.resistor.add(val);
}
//0 entered, so finish the loop
else break;
}
// Ask Which Method To Use,
  

//Output the total resistance
System.out.printf(" The Total Resistance is "
+ f.format(myRes.calculateResistance()));
series=myRes.calculateSeriesResistance();
parallel=myRes.calculaParallelResistance();
System.out.println(" Total Resistance in Series : " + myRes.calculateSeriesResistance());
System.out.println("Total Resistance in Parallel : "+myRes.calculaParallelResistance());
System.out.println("Total Combined Resistance is "+(myRes.calculaParallelResistance()+myRes.calculateSeriesResistance()));
  
       ClosingFrame mainFrame   = new ClosingFrame();
       mainFrame.setVisible( true );
   }
}