Question

The following code must be written and run with no errors in java eclipse Description of...

The following code must be written and run with no errors in java eclipse

Description of the code to be written:

A resistor is an electronic part colour-coded with three coloured bands to indicate its resistance value. This program returns the resistance of a Resistor object instantiated with three Strings, representing the three colour bands of a real resistor.

Start by creating an abstract class 'AbstractResistor' that implements the Comparable interface. This class should have the following three private methods:

-firstBand: String
-secondBand: String
-thirdBand: String

along with public setters and getters for each String.

Now add a concrete class, Resistor, that extends from AbstractResistor. This class should have three public methods:

+getValue(colour: String): int
+getResistance(): int
+compareTo(obj:Object): int

along with a Resistor constructor that takes, and sets, the three String parameters corresponding to the three bands indicated above.

The first of the three methods, getValue(), takes in a single String as its parameter. The String will always be one of the following Strings, and you code must return the integer next to it:
   "Black"   0
   "Brown"   1
   "Red"   2
   "Orange"   3
   "Yellow"   4
   "Green"   5
   "Blue"   6
   "Violet"   7
   "Grey"   8
   "White"   9

To implement this, you must declare an ArrayList of type String and load it with the 10 Strings listed; the index of the String should return the value associated with the String passed as a parameter. Thus getValue("Red") returns '2'.


The second method, getResistance(), returns the resistance of the current Resistor object based on the colour of the three bands. The equation for the resistance is:

(firstBandValue * 10 + secondBandValue) * (10 ^ (thirdBandValue))

where the band values are just the integer values returned by getValue() for the colour String input. Thus for a Resistor instantiated with the three Strings "Brown", "Red", "Red", the value returned by getResistance() would be :

(1 (Brown) * 10 + 2 (Red)) * (100) (i.e. 10 raised to the power of "Red" = 2) = 1200.

You can use Math.power(10, getValue(getThirdBand())) for the last part of the calculation.


Finally, the third method, compareTo(), must return a number less than, greater than, or equal to 0, suitable for use in searching and sorting. (You'll want to use the getResistance() method to do this correctly.)

Now add the following third class to your program and run it.

import java.util.ArrayList;
import java.util.Collections;

public class ResistorTester {
   public static void main(String[] args) {
   ArrayList<Resistor> arResistors = new ArrayList<>();
   arResistors.add(new Resistor("Grey", "Green", "Brown")) ;   //850
   arResistors.add(new Resistor("Red", "Black", "Black")) ;    //20
   arResistors.add(new Resistor("Brown", "Black", "Red")) ;    //1000
   Collections.sort(arResistors);
   for (Resistor r: arResistors) System.out.println(r.getResistance());
   }
}

You should see the numbers 20, 850, and 1000 output, in that order.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

first we are use three private methods :

1] firstBand: String

2] secondBand: String

3] thirdBand: String

then we add concrete class and use three public methods

1] getValue(colour: String): int

2] getResistance(): int

3] compareTo(obj:Object): int

then we use 10 strings and interger number of this string

"Black"   0
   "Brown"   1
   "Red"   2
   "Orange"   3
   "Yellow"   4
   "Green"   5
   "Blue"   6
   "Violet"   7
   "Grey"   8
   "White"   9

then we use first public method

getValue("Red") returns '2';

the resistent equation is :

(firstBandValue * 10 + secondBandValue) * (10 ^ (thirdBandValue))

the Resistor instantiated with the three Strings "Brown", "Red", "Red", the value returned by getResistance() method

(1 (Brown) * 10 + 2 (Red)) * (100) (i.e. 10 raised to the power of "Red" = 2) = 1200

or

You can use Math.power(10, getValue(getThirdBand())) for the last part of the calculation

finally we use compareTo() method and return the number less than or greater than or equal to zero

import java.util.ArrayList;
import java.util.Collections;

public class ResistorTester

{
   public static void main(String[] args)

{
   ArrayList<Resistor> arResistors = new ArrayList<>();
   arResistors.add(new Resistor("Grey", "Green", "Brown")) ;   //850
   arResistors.add(new Resistor("Red", "Black", "Black")) ;    //20
   arResistors.add(new Resistor("Brown", "Black", "Red")) ;    //1000
   Collections.sort(arResistors);
   for (Resistor r: arResistors) System.out.println(r.getResistance());
   }
}

the output is

850

20

1000

Add a comment
Know the answer?
Add Answer to:
The following code must be written and run with no errors in java eclipse Description of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are seven parts for one code, all are small code segments for one question. All the 'pets' need to 'speak', every sheet of code is connected. Four do need need any Debugging, three do have problems that need to be fixed. Does not need Debugging: public class Bird extends Pet { public Bird(String name) { super(name); } public void saySomething(){} } public class Cat...

  • Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement...

    Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • Need code written for a java eclipse program that will follow the skeleton code. Exams and...

    Need code written for a java eclipse program that will follow the skeleton code. Exams and assignments are weighted You will design a Java grade calculator for this assignment. A user should be able to calculate her/his letter grade in COMS/MIS 207 by inputting their scores obtained on worksheets, assignments and exams into the program. A skeleton code named GradeCompute.java containing the main method and stubs for a few other methods, is provided to you. You must not modify/make changes...

  • Comment your code. At the top of the program include your name, a brief description of...

    Comment your code. At the top of the program include your name, a brief description of the program and what it does and the due date. The program must be written in Java and submitted via D2L. The code matches the class diagram given above. The code uses Inheritance. In the following, you are given code for two classes: Coin and TestCoin. You study these classes first and try to understand the meaning of every line. You can cut and...

  • Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • I need help fixing my java code for some reason it will not let me run...

    I need help fixing my java code for some reason it will not let me run it can someone pls help me fix it. class LinearProbingHashTable1 { private int keyname; private int valuename; LinearProbingHashTable1(int keyname, int valuename) { this.keyname = keyname; this.valuename = valuename; } public int getKey() { return keyname; } public int getValue() { return valuename; } } class LinearProbingHashTable2 { private final static int SIZE = 128; LinearProbingHashTable2[] table; LinearProbingHashTable2() { table = new LinearProbingHashTable2[SIZE]; for (int...

  • Hello, Can you answer the following question using the charts and picture I've provided? Thank You...

    Hello, Can you answer the following question using the charts and picture I've provided? Thank You so Much! Information Needed to Solve the Question (2) The two resistors, Ri and R2, shown at the top of Figure 3(a) will be used in Experımehl i. Using the colour code in Table 1, write the corresponding colour and the digit in the t below for each resistor. See the example in Figure 10. Record the values of Ri and R2 with their...

  • the answer must be in java. thank you Question 2 [8 points] Consider the class Class...

    the answer must be in java. thank you Question 2 [8 points] Consider the class Class A below: class Class A implements Serializable{ int a; static int b; transient int c; String s; // Constructor public Class A (int a, int b, int c, Strings){ this.a = a; this.b = b; this.c = c; this.s = s; Complete the class Test to test the serialization and deserialization of the objects of class Class A. State the possible variable values following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT