|
The Java default constructor is only available if no other constructors are provided.
If you provide a constructor that takes parameters you lose the default constructor and therefore must explicitly define it.
See the last paragraph before the 'overloads' section; http://www.javabeginner.com/learn-java/java-constructors[^]
|
|
|
|
|
Been playing around and changing the Bil1 class return values but nothing have helped.
I did try
Bil1 minBil = new Bil1("VD-12345", "Volvo", 2002, 55, true);
and that worked like an charm.
but then the client program ain't approved. the task is to have the values in the class section "Bil1", not client section "Bil1test"
modified 4-Oct-11 18:49pm.
|
|
|
|
|
Tor Danielsen wrote: Bil1 minBil = new Bil1("VD-12345", "Volvo", 2002, 55, true);
I did put something like that in my initial message (that i then deleted, because I started thinking down the default constructor route).
Tor Danielsen wrote: but then the client program ain't approved. the task is to have the values in the class section "Bil1", not client section "Bil1test"
I haven't got a clue what you mean by that.
I am completely confused what you are exactly wanting now. Basically, things wont work if your method signatures don't match, so you either need to remove the paramaters or create a new empty constructor in the the Class (for the reasons I posted in the the message above).
|
|
|
|
|
What Dave said (in his first reply) is correct and very relevant.
What would be the point of having a class Bil1 with all those variables inside it, if all it does is return some constants??? All Bil1 instances would be equivalent and behave identically.
modified 4-Oct-11 19:54pm.
|
|
|
|
|
A slightly modified version of your program:
public class Bil1{
private final String regNr;
private final String merke;
private final int årsmodell;
private final int hastighet;
private final boolean motorenIGang;
public Bil1() {
this (null, null, 0, 0 false);
}
public Bil1(String regNr, String merke, int årsmodell, int hastighet, boolean motorenIGang){
this.regNr = regNr;
this.merke = merke;
this.årsmodell = årsmodell;
this.hastighet = hastighet;
this.motorenIGang = motorenIGang;
}
public String getRegNr() {
if (this.regNr == null) {
return "Ukjent";
}
return this.regNr;
}
public String getMerke() {
if (this.merke == null) {
return "Ukjent";
}
return this.merke;
}
public int getÅrsmodell() {
return this.årsmodell;
}
public int getHastighet() {
return this.hastighet;
}
public boolean getMotorenIGang() {
return this.motorenIGang;
}
public String toString() {
return "Regnr " + this.getRegNr()
+ ", merke " + this.getMerke()
+ ", årsmodell " + this.getÅrsmodell()
+ ", hastighet " + this.getHastighet()
+ ", motorenIGang " + this.getMotorenIGang()
}
public int hashCode() {
return 7 * ((this.getRegNr().hashCode() +
9 * this.getMerke()) +
3 * this.getÅrsmodell()) +
this.getHastighet() +
(this.getMotorenIGang() ? 1 : 0);
}
public boolean equals(Object obj) {
if (obj == null || obj.getClass() != this.getClass()) {
return null();
}
Bil1 that = (Bil1)obj;
return this.getRegNr().equals(that.getRegNr()) &&
this.getMerke().equals(that.getMerke()) &&
this.getÅrsmodell() == that.getÅrsmodell() &&
this.getHastighet() == that.getHastighet() &&
this.getMotorenIGang() == that.getMotorenIGang();
}
}
public class Bil1Test {
public static void main(String[] args) {
Bil1 minBil = new Bil1("VD-12345", "Volvo", 2002, 55, true);
System.out.println(minBill);
}
}
Now, the lessons are:
- Member variables should be private. Just don't go any other way without very good reason, you don't have one.
- Try to have all constructors lead to a single one with all - or most - of the logic. If you wish to change the way it is initialising the class, then only one place needs top be changed.
- Internally use methods rather than variables. It is possible that you need to change the internal representation of a member variable but not the method. This ensures that the methods always get what is expected - design by contract - as you'll see from the way I've changed the methods for make and model.
- If you need to describe an object, let the object do it. Every class should override toString , equals , and hashCode .
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Your code makes no sense; you have a constructor that takes 5 values and stores them within your object, but your property getters all return static values. So any object of the class will always be a 2002 model Volvo with reg number VD-12345. You should change your getters to return the values stored by the constructor. You should also not try to instantiate an object using a constructor (the no parameter version) that does not exist in your class. You can find more details about classes, objects and constructors here[^].
|
|
|
|
|
PLEASE
Use a IDE for your development. Eclipse and Netbeans are FREE - there is no reason to not use them.
And if you'd use them you would have seen your failures right away.
regards Torsten
I never finish anyth...
|
|
|
|
|
Often 'skool kidz' are encouraged not to use IDE's as it can actually impair their learning the language. There is one, BlueJ[^], that is specifically designed for learning Java. It provides good build tools but takes away 'cheats' like auto-suggestion and intellisense. It has added features such as being able to create objects on the fly, great for testing methods. It is a very good learning aid and I would seriously recommend this for students over NetBeans or Eclipse so they can learn the language; rather than the IDE.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
I started my Java programming using vi and progressed to emacs, and used javac to compile. There was nothing wrong with doing it that way, and it was just as easy to find and fix syntax and other errors. But I agree, an IDE is much easier.
|
|
|
|
|
you started with vi and emacs because there was no IDE at that time. That's a difference.
regards Torsten
I never finish anyth...
|
|
|
|
|
Very true, but it's still possible to learn Java (and indeed any other language) without an IDE.
|
|
|
|
|
I started Java programming using Notepad for coding and the command line to compile things. I'm glad I did because I learned a lot about the language that way, looking up Java In A Nutshell to find out what method to use. I still subscribe to the view that if you are new to the language, using an IDE like Eclipse or NetBeans can hinder your understanding of things. Swing programming is a good example: IDEs allow you to draw the layout you want visually, and then it generates the code. Faster and less error-prone, but if that's all you ever do it will take you longer to learn the nuts and bolts of how Swing actually works.
On the other hand, I have never heard anyone heard anyone suggest people should learn C# that way. The idea that people should use Notepad to learn C# before starting with Visual Studio wouldn't really be taken seriously by anyone. I'm not really sure what that says about learning Java as opposed to learning C#.
|
|
|
|
|
It doesn't matter if a WYSIWYG-Editor is included - you need to know how to use such something too. It just positions the GUI-components, that's all. You still need to know for example what a GridLayout is.
Also do learners often not even use a GUI - they just push some command lines around.
regards Torsten
I never finish anyth...
|
|
|
|
|
TorstenH. wrote: You still need to know for example what a GridLayout is.
Well, interestingly NetBean's visual designer (Matisse) is based around a layout called GroupLayout which is not really intended to be hand-coded. The idea is to get away from requiring the developer to know all the ins and outs of the layout and make it easier to design UIs visually.
One way to look at it is a move towards the model of something like C#, where developers don't need to know how the GUI is constructed, just how to wire the components together and respond to events.
|
|
|
|
|
David Skelly wrote: One way to look at it is a move towards the model of something like C#VB, where developers don't need to know how the GUI is constructed, just how to wire the components together and respond to events.
Yea, that worked really well didn't it...
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
use the default constructor as
pubilc bill()
{
}
inside the class bill
|
|
|
|
|
Hi all,
I need a sample working CODE OR PROGRAM for this java - eclipse ,
Search Service - customization in oracle Customer Care and Billing .
|
|
|
|
|
Start here[^]
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Hi... I wanna make a Java application that controls the actions of a poker table as "FOLD", "CALL", "BET X amount," [with keyboard or mouse] the room is BetMost belongs to the iPoker network, but I have no idea how it could do, someone could give me some ideas? ... Thanks
|
|
|
|
|
Start here[^], write some code, and come back when you have a more specific question.
|
|
|
|
|
Are you going to all of the stuff at once. Seems to me you don't have a better idea about the project. Break into simple steps and start work on it. If you don't have a better idea about different subject matters, better read about them and write some sample codes.
The project you are talking about is quite difficult to carry on without a proper subject knowledge.
I appreciate your help all the time...
CodingLover
|
|
|
|
|
Hai , i got a probleam with my coding .The error which i got is cannot symbol.Please correct my code . I also highlight which line give me an error
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Jantina extends Frame implements ActionListener {
JButton Btentu;
JTextField Tnoic, TJantina;
JLabel Lnoic, LJantina,LTajuk;
public static void main(String args[])
{
Jantina j = new Jantina("Jantina");/* error cannot find symbol*/
j.addWindowListener(new WindowEventhandler());
j.setSize(200,250);
j.setVisible(true);
}
public void Jantina(String str)
{
Super(str);/*error cannot find symbol*/
setLayout(new FlowLayout());
setLayout(new FlowLayout(FlowLayout.Center));/"error cannot find symbol*/
LTajuk = new JLabel("No Kad Pengenalan :");
Tnoic = new JTextField(15);
LJantina = new JLabel ("jantina");
TJantina = new JTextField(15);
Btentu = new JButton("Tentu");
}
public void actionPerforemed(ActionEvent e)
{
Object pilihan = e.getSource();
long nilai, noic;
noic = Long.parseLong(Tnoic.getText());
nilai = noic%2;
if ( nilai == 0)
TJantina.setText("Perempuan");
else
TJantina.setText("Lelaki");
}
class WindowEventhandler extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
|
|
|
|
|
Actually the line given below is cause of error
public void Jantina(String str)
{
Super(str);/*error cannot find symbol*/
it should be written as follws
public Jantina(String str)
the void keyword should be removed bcz constructor should naot have any return type
|
|
|
|
|
This is your new compiled Code :
********************************************************************
//Hai , i got a probleam with my coding .The error which i got is cannot symbol.Please correct my code . I also highlight which line give me an error
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Jantina extends Frame implements ActionListener {
JButton Btentu;
JTextField Tnoic, TJantina;
JLabel Lnoic, LJantina,LTajuk;
public Jantina(String str)
{
super(str);/*error cannot find symbol*/
setLayout(new FlowLayout());
setLayout(new FlowLayout(FlowLayout.CENTER));
LTajuk = new JLabel("No Kad Pengenalan :");
Tnoic = new JTextField(15);
LJantina = new JLabel ("jantina");
TJantina = new JTextField(15);
Btentu = new JButton("Tentu");
}
public void actionPerformed(ActionEvent e)
{
Object pilihan = e.getSource();
long nilai, noic;
noic = Long.parseLong(Tnoic.getText());
nilai = noic%2;
if ( nilai == 0)
TJantina.setText("Perempuan");
else
TJantina.setText("Lelaki");
}
static class WindowEventhandler extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
// alll closing here
public static void main(String args[])
{
Jantina j = new Jantina("Jantina");/* error cannot find symbol*/
j.addWindowListener(new WindowEventhandler());
j.setSize(200,250);
j.setVisible(true);
}
}
**************************************************************
|
|
|
|
|
Please use the <pre> tags so the code is readable.
|
|
|
|