I have an ArrayList of employee and ArrayLsit of bosses, and I want to keep those people in a temporary ArrayList , then ordain alphabetically by name, to sort I use the interface comparator.
The problem comes when I will order 2 ArrayList(workers and bosses), because every time I call I use these functions or not is ordered (sortByNameAlphabetical())
public class Employee {
private String name;
public String getName() {
return name;
}
@Override
public String toString() {
return name ;
One of the random number generators in Java extract the higher-order bits of the random number in order to get a longer period.
I'm not sure if I understand how this is done. Suppose that the random number r = 0000 1100 1000 1101. If we extract the 16 most significant bits from r; is the new number r = 0000 1100 or r = 0000 1100 0000 0000?
I have stumbled onto a problem with ArrayLists (not sure if nested ArrayList objects would be a more accurate description) ....
As simply put as I can describe it, I have an Inventory class which creates an ArrayList of a Product superclass, which has two subclasses, PerishableProduct and ItemisedProduct.
By default, the program is meant to have a starting inventory, which is why I have added them in the constructor
public class Inventory { private List<Product> products; public Inventory() { addProduct(new Product("Frying pan", 15, 20)); addProduct(new PerishableProduct("Apple", 5.8, 30, 7)); addProduct(new ItemisedProduct("Cereal", 5.8, 0)); // this is where I am having problems. Wanting to add // objects to the ItemisedProduct's ArrayList for cereal. }
Within the ItemisedProduct subclass is yet another ArrayList which is meant to hold serial numbers.
public class ItemisedProduct extends Product { private ArrayList<String> serialNumbers = new ArrayList(); public ItemisedProduct(String name, double price, int qty) { super(name, price, qty)
[Code] .....
My problem is that I do not know how to populate the serialNumbers ArrayList from the Inventory class' constructor. Because technically, quantity is defined by how many serial numbers are created.
I came across this code which proves that variable initialisation occurs before even constructors are called.However, I am confused over some things.
Firstly, from my understanding, when a new House object is created in this line House h = new House(); , the no-arg constructor of the House class is called.
Since the no-arg constructor House() is called, why are all the creation of Window objects being run first ? Shouldn't Java jump straight into the no-arg constructor House() ?
class Window { Window(int marker) { System.out.println("Window(" + marker + ")"); } } class House { Window w1 = new Window(1); // Before constructor House() { // Show that we’re in the constructor: System.out.println("House()"); w3 = new Window(33); // Reinitialize w3
im new to java and i wanna make a program using java that can add order entries for a restaurant but how to... i was hoping something that would ask to person to input one or two items and before it calculates the total amount to be paid it will ask the guy who inputs the amount whether or not there are additional orders or not before it prints out the total amount... I am pretty sure it will need an if-else statement..
import javax.swing.*; import java.util.Scanner; public class OrderCalc { JFrame.setDefaultLookAndFeelDecorated(true); JFrame aFrame = new JFrame("OrderCalc"); aFrame.setSize(500,250); aFrame.setVisible(true);
I'm learning the swing options of Java, and my first exercise was the typical Chat Room. I got everything right: TextField, TextArea, the jpanel at left side and the buttons inside of it. BUT I can't order the 2 buttons (1 north, 1 south). I've already looked for answers, but didn't find anything and I tried to asked the teacher, but she was too lazy to look for the error. Here is my code:
I am unsure of something. In the following class, which is read first; the static field or the main method?
class Test{ static int a = 3; public static void main(String args []) { //some code} }
I put some code in Eclipse and have tried to look at the hierarchy. It would point to all static fields being initialized in order from top to bottom, including the main method.I had thought that the main method was always the first thing in a public class to be initialized, regardless of where in the code it resides. Am I reading the Eclipse hierarchy wrong? I find Eclipse very difficult, especially since I typically code in Textmate. I just want to see how my code is operated upon,
public class Hello { public static void main(String[] args) { new Student().fun(); } } class Person
[Code] ....
Now the output is "Person". if i hide the "PRIVATE" in class PERSON, that is, method PRINT in class STUDENT override the same method in its father class, then the output is "Student". Why? I mean, how does the program know what PRINT method should be called?
This is what I have to do:Write a program that takes a string of someone's name and prints it out last name first. Your program must use pointers, not array subscripts. You may use the available string manipulation functions if you find an opportunity.
Example:
"George Washington" "Washington, George"
I am not sure how to reverse the name, I have been looking in my textbook and online and cannot figure it out. This is what I have put together so far, it does not run. At the end it says unnecessary return value.
import java.util.*; import java.util.Scanner; public class Test { public static void main ( String [] args ) { Scanner sc = new Scanner(System.in); System.out.print("Enter name: "); String name = sc.nextLine(); String lastname = ""; String firstname = ""; for(int i = 0; i < name.length(); i++) { if(name.charAt(i) == ' ')
I am trying to sort an array that I have by alphabetical order but I am having problems. Firstly the code that I have used to sort the array may not even do what I need but havn't got far enough to test it yet so go easy on me . I have read in some places when searching how to do this that I would have to create my own bubble sort in order to achieve this but I was hoping that Java had a built in sort method/function. Secondly I lack the knowledge in java to be able to assign an existing array or even a variable to the newly sorted array as I need the unsorted version with the original name and the newly sorted version as another.
code (This is not all of the code, I decided to include only what I thought was relevant):
import java.util.Arrays; public class Sentence { private String words[]; public Sentence(String[] words) { this.words = words; } @Override public String toString() { return "Sentence{" + "words=" + Arrays.toString(words) +
[Code] ....
Is it possible to shorten the sort function to just this?
public String sorted() { return Arrays.sort(words); }
I want to write an application that inputs a sentence, tokenizes the words with method split and reverse the words (not the letters)...I am stuck at the last part: how to reverse them...should I use the method reverse(); ?
Here is my code..
import java.util.Scanner; import java.util.StringTokenizer; public class ReversedWords { //execute application public static void main( String [] args) { //get sentence
I am trying to make a program that reads a word and display if the letters are in ascending order or not in ascending order(All kind of letters, capitals ). I have made a code but its not 100% correct. My code:
class Main { public static void main(String args[]) { System.out.print( "#Enter text : " ); String text = BIO.getString();
How do I take the union between an object called on and the object passed in the parameter?I have an interface:
Java Code:
import java.util.Set; /** * An extended Set where additional functionality is added */ public interface NewSet<T> extends Set<T> {
[code]...
Now, the union method requires a NewSet object to be called on. The Union will be performed between the object called on and the object passed in the parameter, which is set.How do I call on a NewSet object in order to take the union between the object called on and the object passed in?Do I just do:
Java Code:
public NewSet<T> union(NewSet<T> set) { NewSet<T> calledOn = new ArrayList<T>(); // ... } mh_sh_highlight_all('java');
I'm trying to make this code's output display like a sentence, since right now it displays downward and doesn' look right.
public class ReverseOrder { public static void main(String[] args) { String phrase = "The rain in Spain falls mainly on the Plain"; char[] phraseArray; phraseArray = phrase.toCharArray(); for(int i = phraseArray.length - 1; i >= 0; i--){ System.out.println(phraseArray[i]); } } // end main } // end ReverseOrder class
the dates are printing out of order. It should appear MM/DD/YYYY however with my code its printing DD/YYYY/MM.Here is my code.
public class DateTest { public static void main(String args[]){ Date date1 = new Date( 7, 4, 2004 ); System.out.print( "The initial date is: " ); date1.displayDate();
what code I should add to my program in order to get these dice to roll. The program, according to my teacher, is CORRECT so far, so I'm not going to change anything. I just need to know how to roll the 2 dice I added in. I just want to get it done. No math.Random. The direction is: Roll each of the Dice by invoking the roll method on each Dice in the array.
My code:
//Dice.java package homework3; import java.util.Random; public class Dice { private int numberShowing; private int numberOfSides; private static final Random randomNumber = new Random();
I am just learning java now i have a problem where i have to run a test order on my internalOrder class from the orderTester class and i,m not sure how to do this ?
This is the code i have for my order class:
public static void testCase8(){ orders ords = new orders ("Pen", 8, -0.5); System.out.println("Test Case 8:" + " " + ords.getOrderDetails());
[Code] ....
This is the code for my Internal class
public final static double DISCOUNT = 0.4; public InternalOrder(String productName,int quantity ){ super(productName, quantity,DISCOUNT); } public void printInternalReport(){ System.out.println("Printing internal report ..."); }
Now my orderTest class is passed though orders class if you need that code i will put it up...
Consider the sequence of digits from 1 through N (N<=9) in increasing order: 1 2 3 4 N
Insert either a +(for addition) or a - (for subtraction) between each of the digits so that the resultant sum is zero. Print all possible combinations that sum to zero.
Example: Enter a number: 7 1+2-3+4-5-6+7=0 1+2-3-4+5+6-7=0 1-2+3+4-5+6-7=0 1-2-3-4-5+6+7=0
I was wondering if there is a way to be certain that listeners would be invoked in any specific order? Note that I am referring to servlet specification version prior to version 3.0. As per version 3.0, the ordering followed would be the one in web.xml (Reference: Servlet specification 3.0: Section 8.2.3). This section also says that prior to 3.0 the order of invocation is random.
Possible duplicate entry but reply does not match the specification text : URL...
I have a problem where i have to run a test order on my internalOrder class from the orderTester class and I am not sure how to do this. This is the code i have for my orderTester class:
public static void testCase8(){ orders ords = new orders ("Pen", 8, -0.5); System.out.println("Test Case 8:" + " " + ords.getOrderDetails());
[Code] ....
this is the code for my Internal class
public InternalOrder(String productName,int quantity ){ super(productName, quantity,DISCOUNT); } public void printInternalReport(){ System.out.println("Printing internal report ..."); }
Now my orderTest class is passed though orders class and intrenalOrder class if you need that code i will put it up.