How To Create And Re-use Object Types
Mar 17, 2014
how objects relate to classes and how you can create and re-use object types.on that point, but this has me baffled. I most certainly do not have a firm grasp yet on passing things to and from methods that just makes my head hurt. SO anyway I tried out one of the code examples:
/* ElectricGuitar.java */
class ElectricGuitar {
String brand;
int numOfPickups;
boolean rockStarUsesIt;
[code]...
But I just realized this thing has no main method and only one class defined.....so I guess I just tried to compile.
View Replies
ADVERTISEMENT
Dec 8, 2014
I need to write a class that represents and image of RGB pixels. This class uses a two-dimensional array of object type that represents R,G and B values.
When I compile the class it completes successfully. However, when I try running a "tester" class , I'm encountering errors in a few methods. I've been trying it out for 2 days now, but unsuccessfully thus far..Those method are : rotating the image by 90 degrees clockwise (and vice versa , but I just use clockwise method 3 times for that) , shifting the image sideways, and shifting the image up/down..
I've attached the whole source code for the class , and the list of errors I get when I try to run my tester. URL...
View Replies
View Related
Aug 18, 2014
I have two different "business objects", and they have multiple attributes in common(around 25 I believe, all of which are simply a String). Basically, these objects are used for documentation purposes on the same file.
The program can choose to update a given Document at any point in time, even if changes haven't been made to existing version. So what I'm trying to do, is check to see if these attributes differ any between the two files(the exisitng copy, and the new request). If so, I'll update...else I simply throw out the request. The workload can be rather intense at times so I don't want to bog down the system anymore then necessary.
Simply pulling down every attribute for each and comparing seems like a lot of overhead, any more efficient way to achieve these results?
View Replies
View Related
Oct 15, 2014
import java.util.*;
public class CommonElements
{
private int comparisons; // number of comparisons made
private Comparable[] arrayToSearch; // current array being traversed
private Comparable[] commonElements = new Comparable[10];
private int arrayPosition = 0; //keeps track of what index to add an element to common at
[Code] ...
I have trying to get this down to the bar minimum. I am trying to cast the desired object array to a array of comparable. This is all required by the assignment.
I am getting a runtime error that I can not perform the desired cast. What do I need to provide the compiler in order to allow for this casting. I can not change the signature of the method however nothing about the class has been specified do I need to implement comparable? Also I don not now what the client is passing so how would I write a generic compareTo method to compare object of unknown types.
View Replies
View Related
May 11, 2015
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.
i.e
UI.initialise;
UI.addButton ("pipe", this::drain);
public void drain(){
this.pipe ("pipe1");
this.pipe ("pipe2");
[Code] ....
When I press the button "drain" it will print=
pipe1
pipe2
pipe3
pipe4
I am having a lot of difficulty just printing one out after each time I press drain.
"drain"
pipe1
"drain"
pipe2 etc..
View Replies
View Related
Apr 9, 2014
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);
}
}
View Replies
View Related
Mar 21, 2014
How many ways to create an object in java ?
View Replies
View Related
Sep 18, 2014
I get an json from http request. I want to create a corresponding Java object.
is there any automated mapping? Something called pojo or something else?
View Replies
View Related
Jul 18, 2014
Why we can't create object of abstract class ,when we can create its constructor?
View Replies
View Related
Jul 4, 2014
//Scanner Test
String stream2 = "ab34ef
56";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(stream1);
// find a string "World"
[Code] ....
Matched expression found by findInLine: 34
ef56
is new line? right?
The java.util.Scanner.nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end.
then 56 is in new line and it must return ef.What is the problem?
View Replies
View Related
Dec 11, 2012
I know that System is a final class and it cannot be instantiated, out is a static final variable of type PrintStream in System class and println is a method in PrintStream class.Still I don't understand why we use System.out to call println() method.To my knowledge a method can be called using an object reference, in case of static behaviors we use classname. Then why here we are using System.out.println? Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.Are there any ways of calling a method apart from those I know(I have mentioned above what I know)?
View Replies
View Related
Oct 5, 2014
Animal ob1=new Lion();
Here we create ob1 for Animal class or Lion class.
public abstract class Animal {
public abstract void eat();
public void breathe(){
[Code] ....
View Replies
View Related
Sep 19, 2014
Programming Assignment #2
(Using an Existing Class: Creating Objects and Calling Accessor and Mutator Methods)
I. The Assignment
This assignment is to write a "test" class (aka: a "driver" class or "client code") that uses the class Balloon.java, available on the class web page.
To use the Balloon class, download it and store it in the src folder of your NetBeans project. Make sure you save it as Balloon.java.
The best way to learn how to use the Balloon class or any other Java class, for that matter - is to consult the documentation, Balloon.html (online). You can also read the javadoc comments that appear just above the class declaration and above each method declaration, which explain what each method does, what the method's parameters are, and what value if any - is returned by the method. The html support pages are generated from these comments.
-Review declaring variables, creating objects, calling methods that return a value vs. void methods, and accessor and mutator methods before beginning. To receive credit for this assignment, you must not modify the Balloon class in any way!
II. Your BalloonTester Class
Your BalloonTester class will have only a single method "main" and will perform each of the following operations, in the exact order listed below. Each operation may be done in one or two statements. Make sure you follow directions faithfully, and note that once you have done step 3, you can copy and paste it to do steps 6, 9, and 12.
1.Create a Balloon object with a name of your own choosing and an altitude of 100 meters.
2.Create a second Balloon object with a name of your own choosing, and specify an initial altitude of -100 meters.
3.Call the accessor methods of the Balloon class to get the name and altitude of each Balloon object. Print the data, one object per line.
4.Make the object you created in step 1 ascend to an altitude of 250 meters.
5.Call the adjustAltitude method to increase the altitude of the object you created in step 2 by 150 meters.
6.Call the accessor methods of the Balloon class to get the name and altitude of each object. Print the data, one object per line.
7.Call the adjustAltitude method to decrease the altitude of the object you created in step 1 by 150 meters.
8.Make the object you created in step 2 descend to the same altitude as the other object. You may assume that the other object is at a lower altitude.
To get credit for step 8., the statement(s) you write must always work, regardless of the actual altitude of the second object. It cannot depend on you knowing the altitude of the second object, but must utilize the fact that the object knows its own altitude. In other words, if you use a literal in any way to set the altitude, it is not correct.
9.Call the accessor methods to get the name and altitude of each object. Print the data, one object per line.
10.Move the object you created in step 1 to an altitude that is four times its current altitude. As in step 8, the statement(s) you write must work for any altitude and may not depend on you figuring out the new altitude beforehand.
11.Attempt to move the object you created in step 2 to an altitude that is 150 meters below its current altitude.
12.Call the accessor methods to get the name and altitude of each object. Print the data, one object per line.
and this is the Balloon.java given:
// File: Balloon2.java
// Modified Balloon class has overloaded constructors
/**
* A class to represent a hot-air balloon. Balloon objects have a name and an altitude.
*/
public class Balloon2
{
// instance variables
private String name ; // name of the balloon
private int altitude; // altitude (height) of balloon in meters
[Code] ....
View Replies
View Related
Apr 16, 2014
I'm curious why ACM isn't letting me create an object from a different class, maybe I am missing something?
my main class:
import java.awt.Color;
import acm.program.*;
import acm.graphics.*;
public class MyClass extends GraphicsProgram {
private static final int WINDOW_W = 500;
private static final int WINDOW_H = 500;
[code]....
I have also noticed when trying to extend 'ConsoleProgram' it does the same thing with line printing (except when using system.out).
View Replies
View Related
Oct 26, 2014
I can't figure out where to create the StringHandler object. My code should take a string as input, then create StringHandler object ord with the string input. This should repeat until cancel is pressed, then ord should be sent to the Utskrift-method (a print method).
If I do like this, null is also sent to Utskrift. I dont want that to happen.
If I put StringHandler last in the loop ord can not be resolved.
String text = "";
while (text != null){
text = showInputDialog(null, "Enter text:");
StringHandler ord = new StringHandler(text);
if (text == null){
[Utskrift(ord.getNumber(), ord.getString(), ord.getWords());
break;
}
}
View Replies
View Related
Feb 26, 2015
I've been writing classes over and over for school. So I create a class outside of my main class. I create a new constructor and then create objects from my main class. I hope that makes sense. So i use methods in that class to work with the object. So I have an object name I've created <dot> method name. So I can create objects and then use methods from the class, but I'm wondering can I create a method in my main class and use it on that object? I don't understand how to do that.
View Replies
View Related
Oct 12, 2014
Write a program that prompts the user for information about some countries, creates an object for each country, and then stores the objects in an array. After the user has entered information about all the countries, your program should print out which countries in the list have the smallest and largest area and population density. Assume the user will enter information about at least one country but that the program will not have to store more than ten countries in the array. The user will indicate that they are done entering countries by typing "DONE" for a country name. Here is an example of what your program must look like when it is executed (user input is shown bold)
Please enter the name of a country: United States
Enter the area in square km and population of United States: 9827000 310000000
Please enter the name of a country: Mexico
Enter the area in square km and population of Mexico: 1973000 122300000
Please enter the name of a country: Canada
Enter the area in square km and population of Canada: 9985000 35160000
Please enter the name of a country: Liberia
Enter the area in square km and population of Liberia: 111370 4294000
Please enter the name of a country: DONE
Liberia has the smallest area at 111370 square km.
Canada has the largest area at 9985000 square km.
Canada has the smallest population density at 3.521282 people per square km.
Mexico has the largest population density at 61.986822 people per square km.
I don't know, I'm really confused with this Java program. I don't know how to do the main part of the program where the user inputs, array, and the output.
// Access the Scanner and ArrayList class by importing the java.util package.
import java.util.Scanner;
/** Project - A Country Object
* The purpose of this program is for the user to enter some information at most 10 countries. After the user has entered information about all the countries, the program should print out which countries in the list have the smallest and largest area and population density.
*/
public class Country
{
private String countryName;
private int theArea;
private int thePopulation;
public Country(String countryName, int thePopulation, int theArea)
[Code] ....
View Replies
View Related
Nov 20, 2014
I am working on a project using pictures and am having some trouble. My assignment is to take a jpeg, and reflect it(over an imaginary x-axis so to speak). As this is my first time working with images, I am very lost. My approach was to take "pixel 0" and have it swap places with "pixel max". Theoretically, I believe I would only need to do this for the first 50% of the pixels, as each flip works on 2 opposite pixels. With my code thus far (the reflect() method), I print the left half of the image. Note: I must use pixel manipulation. I cannot use graphics.
public class MyArt
{
private Picture image;
/**
* Create an Art object using the jpg or bmp at the indicated location
*/
public MyArt(String filename)
{
image = new Picture(filename);
[Code] .....
View Replies
View Related
Jul 30, 2013
My program TestBind0 (code below) tries to find/create a registry and bind an object.
Find/create: it first tests if there is already a registry on that port; if not, then it tries to create one.
The program tries to find/create the registry on ports 40654, 50876, 30321, 33445, 1099, in this order, until it succeeds in both creating the registry and binding the object. Why does TestBind0 throw for each attempt
java.rmi.ConnectException: Connection refused to host: 192.168.1.64; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
[Code] ...
In reg.rebind("TestBind0", obj);even when I have specified -Djava.security.policy==all.policy, with file all.policy in the current dir, containing
grant {
permission java.security.AllPermission;
};
The program is run using command
java -cp bin -Djava.security.policy==all.policy TestBind0
The code:
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.util.*;
public class TestBind0 extends UnicastRemoteObject implements Counter {
private static final long serialVersionUID = 1L;
protected int count;
[Code] ....
View Replies
View Related
Dec 27, 2014
I want to create a program where I need to create an object of list type such as text file will contain nos like 1,2,3,4,5 and write into text file and delete the in FIFO order i.e 1,2,3,4,5...how i can achieve to write a program? I tried bt everytime got concurrent modification exception or Array out of bound exception.
View Replies
View Related
Mar 8, 2015
I have a class of Date with a constructor with 3 parameters in it. Those 3 parameters are int data type just to enter month, year, day.
I have another class called Author which has a constructor of Date diedDate; as a parameter passing to the Author constructor.
I was asked to call the Date parameter is null, call the default constructor but I thought for the Date parameter I could only enter something like 0,0,0 instead of typing in null, null, null because null is for String data type isn't it?
View Replies
View Related
Aug 9, 2014
Essentially, the code I want is:
public void randomCreate(ParentObject obj){
int x = random(0-4); //pseudo
int y = random(0-4); //pseudo
create new ParentObj(x,y);
}
ParentObject is actually abstract, so you would only ever pass one of its children objects to it, and a child object of that type would be created. It seems like there should be a way to pass a type, rather than an object, and then create an instance later down, but I don't know if that is actually possible, or if it is poor programming style.
View Replies
View Related
May 14, 2014
I'm trying to create a table where the Header is a custom object.
The custom object would be something along the lines of
public MyColumnObject
{
String myLabel;
ArrayList dataForDropdowns;
int defaultColumnIndex;
int modifiedColumnIndex;
String tooltip;
}
This is simpler then what I want to do, but it's the basic concept. I want the column header to render the myLabel for the visual (at least at first). I want this render to be applied to however many columns I have, which will differ from table to table, but I always want the column header of each column to be of type myColumnObject.
I thought somethinglike this would work, but I'm getting java.lang.ClassCastException: java.lang.String cannot be cast to myColumnObject which makes sense, but I thought that I would be getting back my object.
p.s. I re-labeled all my code from my actual project, so if something looks off, it was probably just the re labeling.
public NewTestTable(MyColumnObjects[] fields)
{
setModel(new TestObjectTableModel(
new Object [][] {
},
fields
) {
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int rowIndex, int columnIndex) {
[Code]...
View Replies
View Related
Mar 30, 2014
what have I done wrong n the following code? I'm trying to create a new instance carte of object Carti using the constructor and then to insert a row into a table created with SQL.The error I'm getting is:
Exception in thread "main" java.lang.NullPointerException
at Carti.Carti.InsertCarti(Carti.java:103)
at Main.main(Main.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 28 seconds)
The line Main.main(Main.java:37) is when I try to insert the row.
The line Carti.Carti.InsertCarti(Carti.java:103) is when I do the
PreparedStatement st = conn.prepareStatement("insert into Carti (Id,titlu"
+ ", descriere, autor, editie, anPublicare) values (?,?,?,?,?,?)");
Here is the code (Main and Carti Classes)
import Carti.Carti;
import Imprumut_Carti.Imprumut_Carti;
import Membrii.Membrii;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import java.sql.SQLException;
import java.text.DateFormat;
[code]....
View Replies
View Related
Feb 17, 2015
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class App {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please Select an option > ");
[Code] .....
View Replies
View Related
Jul 31, 2014
I'm training myself with the EJB 3 technology. I would like to create a stateless bean that instead of returning a String, it returns an object. I tried in the same way I did with the first exercise, but I'm getting several errors.
View Replies
View Related