Write a Resistor class in java that stores the value of a resistor from the user
ID: 655214 • Letter: W
Question
Write a Resistor class in java that stores the value of a resistor from the user with this method:
public void setValue(int value)
Retrieves it with:
public int getValue()
converts it to color code with:
public void convertColorCode()
then retrieves the color code string with:
public String getColorCode()
Write a main method that allows the user to enter in a resistor value like:
15000
Then convert it to:
For a value of 15000, the color code is Brown Green Orange
Remember, only the first two digits of a resistor are used to make the color code. 15733 would produce the same results as above.
Explanation / Answer
/*
* Nicholas
* ResistorTableFrame.java *
* Assignment 1: Resistors in Parallel
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.TableColumn;
public class ResistorTableFrame extends JFrame implements ActionListener
{
private JMenuItem itmAuthour, itmInput, itmExit, itmHowTo, itmcheck/*deleteme*/;
// Dropdown menu contents private JButton btnClr, btnCalc;
//Defines buttons
private ResistorTableModel resistorTable;
//Defines names for tables ResistorTableFrame()
{
// ============== MENU'S & CONTENTS ==============
// Main menu definitions & names
JMenu mnuFile = new JMenu("File");
JMenu mnuHelp = new JMenu("Help");
// Main menu items
mnuFile.add(itmcheck = new JMenuItem("test"));
/*deleteme*/
mnuFile.add(itmAuthour = new JMenuItem("Author"));
mnuFile.addSeparator();
mnuFile.add(itmInput = new JMenuItem("Input Values"));
mnuFile.addSeparator();
mnuFile.add(itmExit = new JMenuItem("Exit"));
mnuHelp.add(itmHowTo = new JMenuItem("How To"));
// Main menu hotkeys
mnuFile.setMnemonic('f');
mnuHelp.setMnemonic('h');
int keycode = java.awt.event.KeyEvent.VK_A;
int modifier = java.awt.event.KeyEvent.CTRL_MASK;
// Main menu Decloration
JMenuBar mainBar = new JMenuBar();
this.setJMenuBar(mainBar);
mainBar.add(mnuFile);
mainBar.add(mnuHelp);
// Menu selection listening
itmcheck.addActionListener(this);
/*deleteme*/
itmAuthour.addActionListener(this);
itmHowTo.addActionListener(this);
itmExit.addActionListener(this);
itmInput.addActionListener(this);
// ============== TABLE ==============
resistorTable = new ResistorTableModel();
JTable tblTable = new JTable(resistorTable);
// Apply data to table
// ============== PANEL'S & CONTENTS (Buttons & Table) ==============
// BUTTON TEXT
btnClr = new JButton("Clear");
btnCalc = new JButton("Calculate");
// Button selection listening
btnClr.addActionListener(this);
btnCalc.addActionListener(this);
//GUI layout
Container cn = this.getContentPane();
cn.add(tblTable, BorderLayout.NORTH);
cn.add(btnClr, BorderLayout.CENTER);
cn.add(btnCalc, BorderLayout.SOUTH);
}
// ============== MENU & BUTTON ACTIONS ==============
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == itmAuthour)
{
JOptionPane.showMessageDialog(null, "Nicholas");
}
else if (e.getSource() == itmExit)
{
System.exit(0);
}
else if (e.getSource() == itmHowTo)
{
JOptionPane.showMessageDialog(null, " How To Use This Program" + " To insert 'Supply Voltage' and 'Resistor Values' go to"+ " File> Input Values Then use the 'Calculate' button to fill table"+ " Use the 'Clear' button to clear all values in table"+ " IMPORTANT: 'Resistor Values' must be within the E24 range");
}
else
if (e.getSource() == btnClr)
{
resistorTable.resistorClear();
}
else if (e.getSource() == btnCalc)
{
resistorTable.resistorCalculate();
}
else if (e.getSource() == itmInput)
{
resistorTable.resistorInput();
}
/*deleteme*/
else if (e.getSource() == itmcheck)
/*deleteme*/
{
/*deleteme*/
resistorTable.check();
/*deleteme*/
}
/*deleteme*/
}
public static void main(String[] args)
{
ResistorTableFrame app = new ResistorTableFrame();
app.setTitle("Resistor Evaluator");
app.setDefaultCloseOperation(ResistorTableFrame.EXIT_ON_CLOSE );
app.setSize(400, 187);
app.setResizable(false);
app.setVisible(true);
}
}
TRY THIS ONE ALSO:- second file
/*
* Nicholas *
ResistorTableModel.java *
* Assignment 1: Resistors in Parallel *
/ import javax.swing.table.AbstractTableModel;
import javax.swing.JOptionPane;
class ResistorTableModel extends AbstractTableModel
{
java.text.DecimalFormat df = new java.text.DecimalFormat("0.");
//decimal format
private Float RT,R1,R2,R3,VT,V1,V2,V3,C,PT,P1,P2,P3;
private String sR1,sR2,sR3,sVT;
private Object[][] Resistor = { {" ", "Resistance", "Voltage", "Current", "Power"}, {"Totals", RT + " Ohms", VT + " V", C + " mA", PT + " mW"}, {"R1", R1 + " Ohms", V1 + " V", C + " mA", P1 + " mW"}, {"R2", R2 + " Ohms", V2 + " V", C + " mA", P2 + " mW"}, {"R3", R3 + " Ohms", V3 + " V", C + " mA", P3 + " mW"} };
private String[] columnNamesHor= {" ", "Resistance", "Voltage", "Current", "Power"};
private Class[] columnClasses = new Class[] {String.class, String.class, String.class, String.class, String.class};
public int getColumnCount() { return columnNamesHor.length;
}
public int getRowCount()
{
return Resistor.length;
}
public Object getValueAt(int row, int col)
{
return Resistor[row][col];
}
public boolean isCellEditable(int row, int col)
{
return false;
// All cells not editable
}
public String getColumnName(int col)
{
return columnNamesHor[col];
}
public Class getColumnClass(int col)
{
// For cell renderers
return columnClasses[col];
}
public void setValueAt(Object Resistor, int row, int col)
{
// Set value in grid
this.fireTableCellUpdated(row, col);
// Update the table model
}
public void resistorCalculate()
{
JOptionPane.showMessageDialog(null, "How To Use This Program");
}
public void resistorInput()
{
sVT = JOptionPane.showInputDialog("Supply Voltage:");
VT = Float.parseFloat(sVT);
}
public void resistorClear()
{
RT = null;
R1 = null;
R2 = null;
R3 = null;
VT = null;
V1 = null;
V2 = null;
V3 = null;
C = null;
PT = null;
P1 = null;
P2 = null;
P3 = null;
}
public void check()
{
JOptionPane.showMessageDialog(null, "variables float "+RT+" "+R1+" "+R2+" "+R3+" "+VT+" "+V1+" "+V2+" "+V3+" "+C+" "+PT+" "+P1+" "+P2+" "+P3+" "+"variables string "+sR1+" "+sR2+" "+sR3+" "+sVT);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.