Extending Classes In Inheritance Hierarchy
Jan 4, 2015
I have a working program, except that it does not calculate the credit hours and the financial aid. When I enter an input, it works, until it should show the student name, credit hours and financial aid. the error i get from the command is "Hours invalid for false student". Here is the program i think i might have the problem.
import java.text.DecimalFormat;
public abstract class Student
{
//initialise variables
String name;
int creditHrs;
[Code] .....
View Replies
ADVERTISEMENT
Jan 30, 2015
My teacher told me : It looks like everything is working except that in the Box, the method returns true if it is not a box.
The only other thing missing is the if statements in the main method using the .equals() method to do the comparisons. But, now I am lost on how to do my if statement.
HTML Code:
private int height;
/**
* Constructor for objects of class box
*/
public Box3(int l, int w, int h)
{
// call superclass
super(l, w);
// initialise instance variables
height = h;
[Code] ....
View Replies
View Related
Sep 20, 2014
Basically I want to make a class called library, but I don't want to make an interface because I actually want to define the methods. I think I can only use abstract classes but not really sure how to use those. But I still have a problem, I want to create a Map that classes implementing Library class have to have in their code, and the Map will be a HashMap with <String, ParentClassHere>, so basically let's say I make a class called Car, implemeing Library to the Car class would create a Map library = new HashMap<String, Car>. Can I do something like this? And also include methods to get values and set values to the library Map?
View Replies
View Related
Mar 12, 2015
1. Tried to use setLayout() but it wouldn't let me. Have imported libs
import javax.swing.*;
import java.awt.*;
but it doesn't work. It only worked when I extended a class with JFrame. Why do I have to do it? I already have a JFrame instance:
public test(){
JFrame frame = new JFrame();
frame.setTitle("Workshop");
frame.setBounds(500,300,400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
....
}
This code runs smooth, so it's confusing for me that it lets me create a JFrame object, and put buttons onto it, but it wants me to extend the class onto JFrame class in order to use a method.
2. I've read somewhere that AWT is old and should not be used anymore. Is there a diagram or a list with objects and properties that are manipulated only by Swing so that I could concentrate only on those? For example (made up list):
JPaper -
- width, setWidth(x)
- height, setHeight(x)
- position, setPosition(x,y)
- color, setBackground(RGBa)
- acquire, add(subordinate component)
- deprive, remove( subordinate comopnent)
- etc...
JSticker -
- width ...
- height ...
- write, setText(String)
- etc...
View Replies
View Related
Nov 19, 2014
I am writing small pieces of code to make sure I understand Java basics and I have the following.
package teams1;
public abstract class Team1{
private String sport = new String();
public abstract String getSport();
public abstract void setSport();
}
import teams1.*;
[Code] .....
It doesn't compile because sport is private in the super class, but I thought FootballTeam1 would inherit it's own copy of sport because it is extending Team1.
View Replies
View Related
Apr 6, 2014
I am trying to figure out how I can most easily make it easier to make new types of units in my game. I have buildings and ships, and would like to know how I could make it easy to add new units. I have been recently told about interfaces, and have worked with inheritance a little bit.
What I would like to able to do is have it so that all of the variables and methods common to all ships could be stored in a superclass or interface, and same with the buildings. I would also like to be able to assign behaviours to the buildings and ships, maybe as interfaces, which could contain all of the methods and variables required for the functions of that ship or building.
For example, creating a new type of building that can shoot, build ships, and can regenerate nearby ships. So it would possible inherit all of the variables and methods common to all buildings, such as health, image, x, y, getX(), getY() etc. But it would then also gain the variables and methods essential for its functionality, such as shootRange, shoot(), regenRate, etc.
How could this best be achieved?
View Replies
View Related
Aug 16, 2014
I studied that java does not support multiple inheritance using classes. It is fine practically, but I have still a question in mind:Lets see the below code.
class Test{
------
------
}
class MyTest extends Test{
---------
---------
}
Here, as we know that that Object is Super class for every class in java. and I have already extends a Test class in MyTest.My question is: How is it possible to extend features of two class at the same time?
Theoretically,
MyTest extends Object &
MyTest extends Test.
View Replies
View Related
Apr 3, 2014
I have next xml stucture:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<services>
<people>
<name>Julia</name>
<secondName>Roberts</secondName>
</people>
</services>
So, I need to add new <people>...</people> stucture. I made next solution:
NodeList employees = doc.getElementsByTagName("people");
Element emp = null;
for (int i = 0; i < employees.getLength(); i++) {
emp = (Element) employees.item(i);
Element people = doc.createElement("people");
[Code] ....
Then I got
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<services>
<people>
<name>Julia</name>
<secondName>Roberts</secondName>
</people>
<name>Robert</name>
<secondName>Deniro</secondName>
</services>
Yes, I got the new person in my xml file, but it without
<people>...</people>
Where I did mistake in my algorithm?
View Replies
View Related
Feb 9, 2014
I remember reading that a super() call to parent no-argument constructor is automatically inserted by compiler. So, if i have a chained hierarchy of classes (starting at top, with Object), will there be a chain of super() calls, going from bottom to top in the chain ? Will a super() call be inserted in child, if i provide a no-argument constructor for this class ?
View Replies
View Related
Jan 27, 2014
Below I've attached a screenshot of how I've been naming my various java projects as I go through my current textbook. I'm not sure if I'm naming them correctly. I'm on chapter 5 of Introduction to Java Programming by Y. Daniel Liang and he is currently discussing methods and classes. I'm not sure what my projects would be considered (methods, classes, or something arbitrary like projects). Further, if I wrote a program, like loanCalculator215 for example, how could i call that in a different program, like primeNumbers?
View Replies
View Related
Jul 25, 2014
Really need some simple and complete example on how to extends components (graphically and functionally) for primefaces 5.
For example I can't figure how to add custom attributes to existing component or define default values for existing attributes.
Is there any tutorial or a basic common way to achieve this goal or each component have to be extended in its way?
View Replies
View Related
Oct 21, 2014
I am writing a program that should take a url and scan the page for any links. It is in the beginning stages, but I ran into an error when I tried to extend a class. There's a lot going on in this code, but the error is caused by the constructor.
Error message at compile time:
"constructor Page in class Page cannot be applied to given types;
{//Constructor
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length".
Here's the code(first my class, then the class I extended)
public class SearchEngine extends Page {
public static Color customGreen = new Color(69, 194, 33);
public static Color customYellow = new Color(232, 166, 12);
public static Color customBlue = new Color(25,97,255);
public static Color customYellowComp = new Color(178,125,0);
[code]...
View Replies
View Related
Apr 10, 2014
Java Code:
public class Game extends Canvas implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public Game() {
setMinimumSize(new Dimension(width, height));
setMaximumSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
}
[code]...
I want to remove the extends Canvas from the top line and when I use JFrame to add canvas, I don't do add.(this, but instead I do add a canvas variable I make. So instead of extending I want to make a variable. But then how would I do the start, stop and run?I want to use Graphics, Canvas (Not Jpanel because I want to use BufferedImage) & JFrame. I don't want to extend Canvas, how could I use Canvas identically to shown above but instead of extending using it as a variable? or, how could I do this?
View Replies
View Related
May 26, 2014
I'm having a problem with SplitPane (horizontal Flow). What I'm looking for is to find a way to maintain the left pane in the same position when the main window maximizes. I do not want the left pane to grow on its width. If I go into the AnchorPain Constraint in the Scene Builder and remove the right and left constraints, when I run the application, the left pane keeps its position, but it appears a gap between the two panes (the left pane and the right one).
I need to find the way to expand the right pane to the position where the left pane is.
The link shows an image that illustrates the problem. [URL]
View Replies
View Related
Oct 18, 2014
What is the difference between extending JFrame in one class and simply constructing a new JFrame object in that same class? What benefits do I have with each solution, providing I want to use that class to create the GUI. Is it the same or are there differences rather than not having to reference to a new JFrame to be able to use its functions?
View Replies
View Related
Feb 14, 2014
I am new to java and during my classes we built a basic calculator. I want to extend my calculator and added function to square numbers.Here is my program:
Java Code:
package calculator02;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
[code]...
View Replies
View Related
Apr 26, 2014
I've been trying to pull data from another class file "Calculations.java" and have it be displayed in a TextField in "DataAnalyzerGUI.java". Here is how the hierarchy is broken down for my assignment:
DataAnalyzerGUI.java extends JFrame
ReadFiles.java extends DataAnalyzerGUI.java
Calculations.java extends ReadFiles.java
Everything displays and functions correctly in a command prompt if I use a line like this:
System.out.println ("Lowest opening " + dateArray[lowestOpenIndex] + ": " + dataArray[lowestOpenIndex][2]);
But trying to get it to display in a GUI has been quite troubling.
I know the code is supposed to look something like this:
dataOutput.setText(DESIRED CODE HERE);
But I am unable to find anything of value to work out.
I have attached my complete project....
Attached File(s)
Assignment 4.pdf (247.05K)
Assignment 4.zip (235.81K)
View Replies
View Related
Jul 3, 2014
I am working on a project involving a class that has the attributes of one of its inner classes. Now, if possible, I would like to make it so that the inner class is not visible outside of the class. Also, some of the functional mechanics require that the class be an instance of the nested inner class (it extends the inner class). The following code snippet demonstrates the situation.
public class A extends A.B {
public static class B { //ideally I would like this to be private/protected.
}
}
When I try to compile this program, I get the error message "Cyclical inheritance involving A." This error does not make much sense because, since the inner class "B" is static, it requires no instance of "A" (it does not inherit from "A" or uses it). My question is "Is it possible to do something similar to this structure?" I have searched many forums in search of the answer but have not found anything that attempts to explain it. The closest problem that I have found is one relating to the inheritance of a nested inner class from another class. I would like to express that the problem that I am having involves a class defined within the inheriting class.
View Replies
View Related
Jul 11, 2014
I'm learning about inheritance and part of my problem is to create an Order with methods, then an UpdateOrder where the total price is changed by adding four dollars to it, and then a main method displaying a few orders. I've copied all three below in order. My question is when I run the program it will display the totalprice() first for the second order followed by name, number, etc.what you override always displayed first regardless of the order you put them in? (The issue is at line 31 on the third code.)
import javax.swing.JOptionPane;
public class Order { //superclass
private String customerName;
private int customerNumber;
protected int quantityOrdered;
protected double unitPrice;
protected double totalPrice;
[code]....
View Replies
View Related
Dec 16, 2014
I am new to java i dont understand the difference between the abstract and inheritance i mean we use the abstract class with extends with other class name ,even we do that same in the inheritance pls tell me main difference between abstract and inheritance...
View Replies
View Related
Jun 10, 2014
how inheritence and exception work together ?? what are the rules ??
View Replies
View Related
Apr 17, 2014
If I define a class which contains a few static fields, and then have a few classes who inherit this class, then all these classes would have the static field as well. Now my question is the following: would all those sub classes (and the base class itself) share the same object, or would each class have one object for all it's instances?
View Replies
View Related
Feb 24, 2014
I've tried to write a package and two classes this way:
<path>/pack/Aclass.java
Java Code: package pack;
public class Aclass<T> {
private T t;
public void set(T t) {
this.t = t;
[code]....
View Replies
View Related
Apr 24, 2014
I am in an intro programming class and we got assigned a problem for creating a super class with about a dozen sub classes for generating a random word(via WordGetter class) and then comparing that word to a variety of different patterns(like: does the word contain "re"). We were given the super class which looks like this...
public class Pattern {
public boolean matches(String text) {
return true;
}
public String toString() {
return "(TRUE)";
[code]...
and from this class, we have to write subclasses that override those three methods. I am struggling to understand inheritance and I am not really sure where to even start. Here is the instructions for the first sub class we need to write...
"CONTAINS" SUBCLASS
Constructor: The constructor accepts a String named ‘letters’.
Matches: This pattern matches any text that contains at least one occurrence of each ‘letter’.
toString: produces the text “(CONTAINS <LETTERS>)” where <LETTERS> is the ‘letters’ string.
getLetters(): this method must return letters.
equals(Object): careful on this one. Two Contains are equal if they have the same letters (order is not relevant).
(Example):
Pattern p = new Contains(“re”);
boolean f1 = p.matches(“renew”); // f1 is true
boolean f2 = p.matches(“zoo”); // f2 is false
String s = p.toString(); // s is “(CONTAINS re)”
boolean f3 = p.equals(new Contains(“er”)); // f3 is true.. really..
View Replies
View Related
Jul 6, 2014
The first is clear , new Person().printPerson(); displays Person but for the second : new Student().printPerson(); it accesses the Student constructor that points to the Person class => object. It builds the Person instance then goes back to the Student constuctor .Both methods are private and to my knowledge invisible one to the other , except that you cant run the the Person one because it's private so the only one in the Student class is the Student one . Guess it 's incorrect , but why ? (is because private methods cant be overriden and somehow the super class one always has priority ? , even if it's private?)
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
[code]....
View Replies
View Related
Jun 25, 2014
If i have 2 classes, Top and ClassB which extends Top
public class Top {
String variable;
public Top(){
setVariable();
}
void setVariable(){
variable = "variable is initialized in Main Class";
[code]....
So what is happening when ClassB inherits from Top?I know that the B constructor is calling super, so does that mean its calling setVariable (in Top?) but as its overridden in ClassB, then that is whats being called and setting the String variable?
View Replies
View Related