I would like to know what is the significance of instantiating an object without an assignment.
I have created a class TestClass1 with a single constructor that prints a test message.
In TestClass2, if I write "new TestClass1()" rather than "TestClass1 x = new TestClass1()" it still works, and prints the test message.
I would like to know if I do not assign an object at the time of instantiation, it cannot be referenced or reused later, then what is the significance of this type of construct and when it can be useful, and where is the object being held.
public class TestClass1 {
TestClass1() {
System.out.println("This is a test message");
}
}
public class TestClass2 {
In summary, the Class XCopy's Main method creates an instance of the XCopy Class. So, now knowing that this can and does occur my next questions to myself were:
When would I do this? How can this best be used?
Is this just another option available to a Java developer that has no other special significance?So far have no answers for myself.
I have two classes, MonsterGame (shown below) and Monster. Monster has a public accessor method to get it's private character variable, which represents the first character of the name of the Monster in question.
I'm just a bit confused as to why am I unable to cycle through the array of Monsters I have in the MonsterGame class and call
m.getCharacter(); (pretty much the last line of code) public class MonsterGame{
private static final char EMPTY_SQUARE = ' '; private char[][] gameBoard = new char[10][10]; private Monster[] monsters = new Monster[4]; private int maxXBoardSize = gameBoard.length -1; private int maxYBoardSize = gameBoard[0].length -1;
[Code] ....
I understand that there need to be instances of objects to call methods, but is that not the case here? the Monster objects are have already been created, no? Do I need to create an index for the array? is the for loop not enough?
I just cant seem to understand the order of precedence here.
class Test{ public static void main(String[] args){ int k = 1; k += (k = 4) * (k + 2); System.out.println( k ); } }
From what I have read compound operators have the lowest order of precedence... But the above piece of code makes the assignment k = 1 + (k = 4) * (k + 2) before evaluating the rest of the statement first.
It then evaluates (k = 4) and proceeds with the remained of the statement 1 + 4 * (4 + 6)....
I dont understand why the first k is assigned 1 but the remaining ks 4. Should they not all be 1 or 4 (I would have thought 4, since += has the lost order of precedence so is evaluated last)??
I am working on the following java assignment..Write a program that randomly fills in 0s and 1s into a 4- by- 4 matrix, prints the matrix, and finds the first row and column with the most 1s. Here is a sample run of the program:
0011 0011 1101 1010
The largest row index: 2 The largest column index: 2
I have code that generates random 0s and 1s for the array, how to get the largest column and row.
import java.util.Random; public class LargestRowColumn { public static void main(String[] args){ //create 4x4 array matrix int arrayMatrix[][] = new int[4][4];
[code]....
finding the row and column with the largest amount of 1s. I keep thinking well if I scan and find a one in the array, maybe I can just save the index of the row and column and then determine which index contains the most 1's after the array has been scanned.
I am trying to get items to display that would display in a command prompt now into a GUI. I am freaking lost at the moment, probably because I've been staring at this code for over a week now. I have included all the files that are necessary to run the program as an attachment for your own testing purposes. Should I be using a TextField to display the data from the CSV files? How do I get the data to be displayed? How would I get it to be displayed based on the different files Staples (newSTPL.csv), Apple (newAPPL.csv), and Microsoft (newMSFT.csv)?
Java Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * DataAnalyzer Class * This class instantiates the methods from the ReadFiles Class and Calculations Class.
I have a 2 dimensional array assignment with a loop, i'm supposed find the average score of each student from a grade record and find the average score for each test. I've been trying to do the assignment all day with no progress. This is what i have so far
public class SiuTest { public static void main(String[] args) { String [] stu= new String [5]; int [] [] grade= new int [4][2]; String hold;
I have an assignment to create a Sphere class that will allow you to create Sphere objects using the code below. Then create a program called SphereTester that prompts the user for the radii of two spheres in meters. The program should display which sphere is larger and by how many cubic meters and display only four digits after the decimal point. I have the sphere class given to us for the assignment which is this:
public class Sphere { // instance variable (i.e., a field) private double radius; // constructor for the Sphere class public Sphere(double r) { radius = r;
[code]....
Here is a sample run of what the final result should look like
Enter the radius of a sphere (in meters): 1 Enter the radius of a 2nd sphere (in meters): 2 Sphere 2 is greater than Sphere 1 by 29.3215 cubic meters
I've been working on this same assignment using netbeans. I've completed the exercise and when I run it, it works. However, netbeans still had errors listed in the left hand numerical column and it asks me if I still want to run the program with errors when I hit run?
package TestScores; import java.util.Scanner; // Name: Joe // Date: ........ // Desc: Test Score Averages
[code].....
I found this thread while doing a google search trying to find out the error in my code.
I thought numeric literal were by default int or doubles, depending on if have a . and numbers after the But I wrote a quick test program as listed below. I understand the float float floatA = 5.5; failed to compile since 5.5 is a literal of type double and you are trying to assign this to a floag
What I am having problems with is byte byteA = 5; 5 is a literal of type int and this is being assigned to a byte and compiler should complain.The compiler does not allow two byte values to be added and assigned to a byte since the result of the addition is an int
class literalTesting{ public static void main(String[] arg){ byte byteA = 5; // allowed WHY I thought literal is an int and assigning int to byte byte byteB = 10; // allowed
I understand the stack part of it, just not the loop, specifically how you give the user more than one option to input.
Implement a printer driver that handles incoming print jobs. The driver will behave in the following manner:
- Loop giving the user the following options: ADD print job, DELETE latest print job, and QUIT - Recording the following information from each job: computer name, document name, and number of pages - When the user chooses to ADD, add a new print job to the top of the stack - When the user chooses to DELETE, pop off the latest print job and output to the screen which job has just been deleted - When the user chooses to QUIT, output the print jobs still in the stack (in order from top to bottom) - Output must include all relevant information for each job.
Write a class encapsualting the concept of a course grade, assuming a course grade has the following attributes: a course name and a letter grade. Include a constructor, the accessor and mutator, and methods toString and equals.Write a client class to test all the methods in your class.
package labmodule7num57; import java.util.*; public class LabModule7Num57 { // Constructors//
URL....So the problem is that when I type in "PA" it will display about 24 Zips and Populations before it stops. The problem is in the ZIPs file. It goes down the list and then takes the Zip from the Zips file to the Zips in the Population file and displays the Population. It will go to population 513 and stop. Reason being, there is no ZIP code in the Population file to display a population. The loop then stops. How can I get the program to skip over the zip code when there is no corresponding ZIP code in the other file and continue showing the other Pops..Here's what I currently have completed:
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Population { //Declaring global variables. Scanner fileScannerZip, fileScannerPop, inputFile; private String lineZip, linePop; int invalidZip;
Create an equals method that takes an object reference and returns true if the given object equals this object.
Hint: You'll need 'instanceof' and cast to a (Geocache)
So far I have:
public boolean equals(Object O){ if(O instanceof Geocache){ Geocache j=(Geocache) O; if (this.equals(j)) //I know this is wrong... but I can't figure it out return true; }
else return false; }
I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?
I am new to Java and have read books, the Java docs, and searched the Internet for my problem to no avail. I have an Array of objects that contains strings. How can I get the object's strings to print in a list so that the user can select that object to manipulate its attributes? For example, the user can select "Guitar 1" from a list and manipulate its attributes like tuning it, playing it, etc. I have a class called Instruments and created 10 guitar objects.Here is the code:
Instrument [] guitar = new Instrument[10]; for (int i = 0; i < 10; i++) { guitar[0] = new Instrument("Guitar 1"); guitar[1] = new Instrument("Guitar 2"); guitar[2] = new Instrument("Guitar 3"); guitar[3] = new Instrument("Guitar 4"); guitar[4] = new Instrument("Guitar 5"); guitar[5] = new Instrument("Guitar 6");
Now lets say that I want to access a method 'addInterest()' that is in the 'SavingsAccount' class I would have to do: '((SavingsAccount)s).addInterest();'
The question I have is why do I have to cast 'b' to SavingsAccount? Isn't the actual object reference of 'b' already an instance of 'SavingsAccount' class? How does the 'BankAccount' affect the object itself? I'm really confused as to what class is truly getting instantiated and how BankAccount and SavingsAccount are both functioning to make the object 'b'.
I don't understand why the object reference variable 'a' cannot be recast from a thisA object reference to a thisB object reference.Is it the case that once a reference variable is linked to a particular object type then it cannot switch object types later on.I am facing the Java Associate Developer exam soon and I am just clearing up some issues in my head around object reference variable assignment,
class thisA {} class thisB extends thisA { String testString = "test";} public class CastQuestion2 { public static void main(String[] args) { thisA a = new thisA(); thisB b = new thisB();
I am trying to get this to where I can type in a name and it will search through each object and print back the corresponding object info.
Java Code:
import java.util.Scanner; public class MyPeople { public static void main(String[] args) { Person[] p = new Person[] { new Person("Chris", 26, "Male", "NJ", "Single"), new Person("JoAnna", 23, "Female", "NJ", "Single"), new Person("Dana", 24, "Female", "NJ", "Single"), new Person("Dan", 25, "Male", "NJ", "Single"), new Person("Mike", 31, "Male", "NJ", "Married") };
Task:The main method of the class Things below creates an object called printer deriving from the class PrintingClass and uses that object to print text. Your task is to write the PrintingClass class.
Program to complete: import java.util.Scanner; public class Things { public static void main(String args[]) { String characterString; Scanner reader = new Scanner(System.in); PrintingClass printer = new PrintingClass(); System.out.print("Type in the character string for printing: "); characterString = reader.nextLine(); printer.Print(characterString); } }
// Write the missing class here
Note: In this exercise the solution is part of a conversion unit where many classes have been declared. Because of this the classes are not declared as public using the public attribute.
Example output
Type in the character string for printing: John Doe
John Doe
My Class: class PrintingClass { public void print(){ System.out.println(characterString); } }