Dispose Of Containers Ifs And Elses Using Polymorphism
May 31, 2014I would eliminate the ifs and elses using polymorphisms that solves this lot of ifs? How could I solve this?
View RepliesI would eliminate the ifs and elses using polymorphisms that solves this lot of ifs? How could I solve this?
View RepliesIt seems we have abandoned Dice/Die(s), and are now working on something completely foreign. I don't even have a code to start with because I haven't the faintest clue what is going on here (no notes given on this topic, as usual). We are given 4 half-written programs to work with.
The instructions are:
"Examine the FormLetterEntry abstract class, and create the two derived classesTextEntry and DataItemEntry. Be sure to implement all the abstract methods in each derived class."
This is the code we were given:
package homework5;
import java.util.Properties;
/**
* Abstract class representing the entries in a form letter.
**/
public abstract class FormLetterEntry {
/**
* Retrieve the template string for this entry
* @return the value of this entry in a template
**/
[code].....
I understand (and correct me if I am wrong) that a derived class is a class that is created from a base class via inheritance. What I don't see are any notes on how to write a derived class. I see some notes online on how to do so, but they don't fit with what he's written above. What he means by "Be sure to implement all the abstract methods in each derived class."
Currently I get errors when trying to exit my JFrame without playing the youtube video I have set it to load.
My Code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
[Code] ....
If it's relevant the error:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
[Code] .....
Is it possible to dispose(); an internal frame and keep my main JFrame open.
View Replies View RelatedI have this code:
RegistraFolios dg = new RegistraFolios();
dg.dispose();
dg.setUndecorated(true);
dg.setLocationRelativeTo(null);
dg.setVisible(true);
JOptionPane.showInputDialog(dg);
in last line i show the form in modal mode.
how to free? i try this:
private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
but not work.
I am trying to code a program which pops up a file chooser window or dialog box to select a file first, if the user clicks 'cancel' or closes the window then the parent JFrame would not show up.
Java Code:
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.UnsupportedLookAndFeelException;
public final class base extends javax.swing.JFrame {
[Code] .....
The problem is that the JFrame shows up even if I click cancel in the file chooser. I am using Netbeans.
This code will not dismiss the MemoryDateDialog when it loses focus, while it is what it is intended to do.
btnMemoryReason.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
List<MemoryDates> lMemoryDates = db.getMemoryDates(custID);
final MemoryDateDialog mdd = new MemoryDateDialog(lMemoryDates);
[URL] .....
How can i attach the OpenGl display to a JFrame and so that when i close the JFrame is destroys the display? Here is my code so far:
package test.core;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.WindowAdapter;
[Code] .....
I had the opengl display attach to the JFrame before i did the runnable. But after adding the runnable the display now shows up the same size as my screen size. I have tried rearranging the
canvas.setSize();
and the
frame.setSize();
but nothing changes the opengl display is still the same size and when i try to close the JFrame first rather then close the display first i get this error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: From thread Thread[AWT-EventQueue-0,6,main]: Thread[main,5,main] already has the context current
which points me to my
Display.destroy();
which im guessing is telling me that i am not properly disposing the display? How to attach the opengl display to the JFrame and fix the error above?
I've a question regarding polymorphism.
Is polymorphism possible only when the superclass is abstract?
While studying polymorphism , i have some doubts that i am unable to clarify ..... Here they are :
Suppose our inheritance ladder be :
C1 <- C2 <- C3 <-....... <- C100
where C1 is most general (superclass) and C100 is most specific class .
Now , if i write java code in my main() :
C21 Obj = new C100();
Obj.someMethod();
So, what will happen in scenarios as given below :
Scenario - 1) If someMethod() is only defined in C1 ? How will compiler search for this someMethod() ?Will it be executed ?
Scenario - 2) If that someMethod() is static and only defined in C1 , then how will it be searched and executed ?
Scenario - 3) If someMethod() is only present in C100 , then what will happen ?
I have difficulty understanding the following behaviour.
class A {
public String string = "a";
public String toString() {
return string;
}
}
class B extends A {
[code]...
The output of the program is 'c'. This is expexted behaviour. But if class B is changed as follows,
class B extends A {
public String string = "b";
}
Now the program prints 'a' instead of 'c'. Why the statement: b.string = "c"; is not taken into account?
I have a program I want to make (text based, no gui). There is the main class, an Employee class (sort of a template), a CrewMember class, and a Manager class.
I'll put the code for each class an explain the problem I have.
package polymorphism;
import java.util.Random;
public class Start {
public static void main(String[] args) {
Random rand = new Random();
Employee staff[] = new Employee[5];
for(int i = 0; i < staff.length; i++){
[Code] ....
Some of the code is a bit incomplete simply because I ran into the problem. As you can see I made an array in the Start class and it holds objects of Employee type, but create a new instance of either a crew member or a manager, sets their wages, hours, and bonus if applicable. I know if I create an array of a certain type, I can't call upon the subclass' method (Manager in this case) because it has a new method that I added. What I'm trying to do is pretty much call upon the getSalary() method in the Manager class/object, but of course I can't. What way would i be able to do that? I tried looking for some answers. I read about making the superclass abstract and implementing it into the subclasses. Would that be an option?
Recently I have been thinking of using additional interfaces in one of my libraries to hide certain "unsafe" methods of my classes from the user. I would like to get the opinion of other, more advanced java programmers on this issue.
What I do is something like the following (heavily simplified):
public interface ReadOnly {
public int getValue();
}
public interface ReadWrite extends ReadOnly {
public void setValue(int value);
[Code] ....
The user would have access to the ExternalInterface. The ExternalInterface is controlling the values of the InternalComponent. The user can get the InternalComponent, but typecasted to a ReadOnly which does not expose the setters.
Now the thing is the user can still access the setValue method by typecasting the ReadOnly to an InternalComponent or to a ReadWrite, and that is not bad. If the user knows exactly what he/she is doing this will give him/her more flexibility and more power. But this way it *should* become obvious that this is not the intended use and perhaps the extra work of typecasting will discourage the user from doing this.
can we achieve runtime polymorphism by data members?
View Replies View RelatedWhy is the equals-method in the super-class invoked? Shouldn't the equals-method in the sub-class be invoked(at least in the first if-statement since b2 is a B(i know B is also an A))?Is the equals-method overridden? Or does B have its own equals-method?
class SomeClass{
public static void main(String[] args) {
B b1 = new B(123,1);
B b2 = new B(123,2);
[code]...
I have a quick polymorphism question. I have a parent class and a sub class that extends the parent class. I then declare an array of parent class but instantiate an index to the sub class using polymorphism. Do I have to have all the same methods in the child class that I do in the parent class? Here is an example of what I mean.
public class ParentClass
{
public ParentClass(....){ }
public String doSomething(){ }
}
public class ChildClass extends ParentClass
{
public ChildClass(....)
[Code] ....
Is polymorphism similar to interfaces where the child class needs all the same methods?