So I have an Enum file with 119 constants and each constant of that type has 20 fields that come with it. All the fields are the same type and named the same (e.g. there are 119 of Object obj, one for each constant), and I want to run the same methods over them. Since the Objects of the same type are named the same for each constant, I just have them named explicitly in get-er methods.
This worked fine when I just put all 20 fields through the constructor and set them as fields under all the constants. But I realized that if I wanted to make an instance of this Enum class, I'd have to enter in all 20 fields when they are all a set of Objects with unique values. So I then put them as fields under their own respective constant to make it easier to create instances of this enum. But now my methods don't work.
A) I don't really understand why they don't work anymore? B) Is there a way to fix it without putting all the methods under each constant?
Example:
public enum MyEnum { AAA { private MyObject obj = new MyObject (3.0); }, BBB { private MyObject obj = new MyObject (1.5); }, CCC { private MyObject obj = new MyObject (6.5); }, DDD { private MyObject obj = new MyObject (3.5); };
public double getObjVal() { return obj.value(); // it can't find this obj should I move it up to where the constants are declared? } }
I have a set of enum values (let's call then ONE, TWO, THREE.....). I want to find the larger of two of them. But max(ONE,THREE) gives a compile error as MAX isn't defined for type-safe enums. Fair enough.
I also agree that one shouldn't be able to use arithmetic functions on enums.
But as Enum implements Comparable, one can write a function which implements max and min, rather inefficiently I assume.
Is there a better way of getting the max/min of an enum? And if not, can the Java team be persuaded to implement it?
If you have an enumeration type with instances whose publicly accepted names are numbers, e.g. a Chevy 396 and I'm sorting this car according to makes and models that I've already laid out within enum types for each. Here the make instance is "Chevy" and the model instance is just "396".
I know I could make this "396" into "Ch396" and this would work programmatically. But this might confuse some people who just expect to see "396".
In short, why are enum instances not allowed to be of String type rather than just standard Java identifiers ?
How would I go about and make an enum, that has Strings and Methods?I want to make a class called GraphicalEffects, this class and be instanstiated and it has a method to apply graphicalEffects AS methods or some type of references to methods in an ArrayList.
import java.util.*;public class DebugSix { public static void main(String[] args) {
ArrayList<String>products = new ArrayList(); products.add("shampoo"); products.add("moisturizer"); products.add("conditioner"); Collections.sort(products);
[Code] ....
I am using netbeans and getting errors for display(); and size(); it is telling me the errors are :
for the display error, "method display in class DebugSix cannot be applied to given types; display();" and for the size() is : "cannot find symbol System.out.println(" The size of the list is " + size());"
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
public class Circle { private double PI = 3.14159; private double radius; public Circle() { radius = 0.0;
[Code] ....
This is the error i am receiving:
Circle.java:78: error: method getRadius in class Circle cannot be applied to given types; System.out.println("A circle with a radius of " + circle.getRadius() + " will have an area of " + circle.getArea() + " , a diameter of " + circle.getDiameter() + " and a circumference of " + circle.getCircumference()); ^ required: double found: no arguments reason: actual and formal argument lists differ in length 1 error
I'm new to programming and I have an assignment due in java class. Here is the error code:
TestCircle.java:10: error: method setradius in class Circle cannot be applied to given types; circle1 = inputCircle.setradius(); ^ required: double found: no arguments reason: actual and formal argument lists differ in length
And here is my code:
import java.util.Scanner; public class TestCircle { public static void main(String[] args) { double circle1; double circle2; double circle3; Circle inputCircle = new Circle();
I am having trouble with methods. What I want to do is be able to create 4 types of strings under the same method, but only draw one of them at a time.
Trying to find a way to use primitive data types to overload sound()method. I can't seem to warp my head around using an int or a double to overload the method. And if I did, how do you call them in the main afterwards?
I am trying to remove the duplicate elements from ArrayList using .contains() if elements are primitive datatype it works but user-defined datatype does not work.
public class UserBean { String name; String address; public String getName() { return name;
I want to have a priceObject which is constructed by a price the enumtype and the name.
public class Testing { public static void main(String[] args) { PreisObject p1 = new PreisObject(1,Price.liquid,"TEST1"); System.out.println(p1); PreisObject p2 = new PreisObject(2,Price('f'),"Test2");
[Code] .....
As you can see with PreisObject2 I want to check the enum by the value and not by the name as in PreisObject1.
Or do I have to use a if-else or switch statement to do something like this?
I am trying to write out a program that takes numerical input from the user and converts it to a date using the English month name. I am experimenting with the method of a "switch" statement without using the "break" clause. However, I seem to be missing something, as Eclipse is telling me I have a syntax error with my block. My curly braces seem properly placed. Also, I made sure to follow guidelines to make my code fit on the screen and remain easy to read.
import acm.program.*; public class MethodsThatReturnNonNumericValues extends ConsoleProgram { public void run() { int month=readInt("Enter month number"); int day=readInt("Enter day"); int year=readInt("Enter year");
I must write a method that accepts a string and returns an int. The method should sum the unicode values of each character, so the string "ABC" should return 198. Also the only string class methods I'm aloud to use are length, charAt, and substring. I just don't know how the get the Unicode value.
Write a method called largerAbsVal that takes two integers as parameters and returns the larger of the two absolute values. A call of largerAbsVal(11, 2) would return 11, and a call of largerAbsVal(4, -5) would return 5.
I have tried this code using methods in the Math Class but I am getting an error in Practice-it that says
Line 4 Your method's return type is void, which means that it does not return a value. But your code is trying to return a value. This is not allowed.
cannot return a value from method whose result type is void return Math.max(Math.abs(Num1), Math.abs(Num2));
Here is my code. What I am doing wrong?
public static void largerAbsVal (int Num1, int Num2) { return Math.max(Math.abs(Num1), Math.abs(Num2)); }
private int coin; Money(int c) { coin = c; } int showCoin() { return coin; }
and for a test class, I need an array list with a couple of coins in it (i.e. ONE_POUND, TWO_POUNDS) and a loop that adds together the values of the coins in the list and prints the result. How can I do this?
Is there anyway to iterate an enum type without an instance. As some context, consider the following code:
Java Code: public interface GenericChecker { public bool isValid(String str); } mh_sh_highlight_all('java'); Java Code: public class EnumChecker<T extends Enum<T> > extends GenericChecker { private Class<T> enumType; //No instance
[code]....
toString method of the enum types has been overridden so that it returns the name assigned to the enum rather than the enum name itself. For example an enum might be SOME_ENUM("Assigned name"), therefore toString returns "Assigned name" rather than "SOME_ENUM". The idea is that a field from a table can be handed to the isValid(String) function on the GenericChecker base, and the derived class will then check to see if the field matches valid data as far as it is concerned.Thus, I can create a whole bunch of checkers easliy:
I just kinda get stuck when it comes to passing values into constructors, using main method or static method functionality. In theory i kind of understand how it work but when i type it, it's totally different! I have to have a junit test too but i guess i could do that in the end.
I have attached the assignment. So, how to proceed with this:
public class Flight { int flight_number, capacity, number_of_seats_left; String origin, destination; String departure_time; double original_price;
I am doing an assignment and I am clueless as to what I did wrong or supposed to do in terms of placement of coding.
This is what I supposed to do:
1.) Type the following shell for the class:
public class ParadiseInfo { }
2.) Between curly braces of the class, indent a few spaces and create the shell for the main() method:
public static void main(String[] args) { }
3.) Between the braces of the main()Method, insert a call to the displayInfo() method:
displayInfo();
4.) Place the displayInfo() method outside of the main() method, just before the closing curly brace for the ParadiseInfo class:
public static void displayInfo() { system.out.println ("Paradise Day Spa wants to pamper you."); system.out.println ("We will make you look good."); }
This is what I attempted to do:
I know it is wrong I am just clueless on where to put the code and why
public class ParadiseInfo { displayInfo(); public static void main(String[] args) { public static void displayInfo(); } system.out.println("Paradise Day Spa wants to pamper you."); system.out.println("We will make you look good."); }
--- Update ---
I also tried it this one and ended up with 1 error..
public class ParadiseInfo { displayInfo(); public static void main(String[] args) { } public static void displayInfo(); { system.out.println("Paradise Day Spa wants to pamper you."); system.out.println("We will make you look good."); } }
I am just not importing the Enum value properly. The error I am getting with the DECK class is
import java.util.ArrayList; public class Deck { ArrayList<Card> deckList = new ArrayList<Card>(); String[] value = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"}; String[] suit = {"Hearts","Clubs","Spades","Diamonds"};
[Code] ....
1 error found: File: C:UsersFouadDesktopSchooCSC 162 SUMMER 14lackjack1Deck.java [line: 19] Error: constructor Card in class Card cannot be applied to given types; required: CardEnum.Rank,CardEnum.Suit found: java.lang.String,java.lang.String reason: actual argument java.lang.String cannot be converted to CardEnum.Rank by method invocation conversion
I have also tried this with the Deck Class
import java.util.ArrayList; public class Deck { ArrayList<Card> deckList = new ArrayList<Card>(); String[] value = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"}; String[] suit = {"Hearts","Clubs","Spades","Diamonds"};
[Code] ....
Resulting ERROR message
2 errors found: File: C:UsersFouadDesktopSchooCSC 162 SUMMER 14lackjack1Deck.java [line: 19] Error: cannot find symbol symbol: variable value location: class CardEnum File: C:UsersFouadDesktopSchooCSC 162 SUMMER 14lackjack1Deck.java [line: 19] Error: cannot find symbol symbol: variable suit location: class CardEnum
THE FULL CODE!(BELOW)
public class BlackJack{ BlackJack() { Deck deck = new Deck(); deck.createDeck(); System.out.println(deck.deckList); } public static void main(String[] args) { new BlackJack();