the right usage of Heridity in Java and the private attribute issue? - java

I need to code a programm and have some issues doing so:
Super-Class:
public class Kunde {
private String name;
private String adresse;
private double marge;
private int nummer;
public Kunde(String name, String adresse, int nummer) {
this.name = name;
this.adresse = adresse;
this.nummer= nummer;
marge = 2;
}
public void print() {
System.out.println("\nKunde: "+name);
System.out.println("Adresse: "+adresse);
System.out.println("Kundennummer: "+nummer);
System.out.println("Marge: "+marge);
}
public double getMarge() {
return marge;
}
public void setMarge(double marge) {
this.marge = marge;
}
public int getNummer() {
return nummer;
}
}
Main-Class:
public class Main {
public static void main(String[] args) {
Kunde k1 = new Geschaeftskunde("ABC AG", "Weg 3", 100,"CHE.123");
Kunde k2 = new Privatkunde("Hans Müller","Nebenstrasse 2", 101);
k1.print();
k2.print();
}
For the Kunde k1 an error occurs: "The constructor Geschaeftskunde is undefined"
But i actually initialized the constructor as followed:
public class Geschaeftskunde extends Kunde {
private String uid;
public Geschaeftskunde (String name, String adresse, int nummer) {
super(name,adresse,nummer);
}
public String getUid() {
return uid;
}
public void print() {
System.out.println("UID: "+uid);
}
}
In the same way I initialized the constructor for Kunde k2:
public class Privatkunde extends Kunde {
public Privatkunde (String name, String adresse, int nummer) {
super(name,adresse,nummer);
}
}
And for this class this error surprisingly doesnt occur?
Question:
How can i use and change the attribute marge in the classes Geschaeftskunde und Privatkunde when its set on private?
I know that you can use public methodes with that attribute so is the code: "super.setMarge(x);" the right attempt in solving this problem?

The compiler error is due to Geschaeftskunde taking 3 arguments while you call it with 4.
Regarding marge you need to use setMarge(x) and getMarge(). The super is not necessary since Geschaeftskunde/Privatkunde inherit the public methods.
Fixed constructor for Geschaeftskunde:
public Geschaeftskunde (String name, String adresse, int nummer, String uid){
super(name,adresse,nummer);
this.uid = uid;
this.setMarge(2);
}
Privatkundewould be similar.

Related

City cannot be converted to summable

So I'm supposed to use a "Summable" interface to add up the populations of the cities. I've been staring at it for an hour but still can't find my error. Please Help!
This is my tester
public class SummableTester extends ConsoleProgram
{
public void run()
{
City cookie = new City("Coookie", 20000);
City taco = new City("Taco", 10000);
System.out.println(taco.getValue());
}
}
City Class:
public class City
{
private String name;
private int population;
public City(String name, int population)
{
this.name = name;
this.population = population;
}
public String getName()
{
return this.name;
}
public int getValue()
{
return this.population;
}
public int add(Summable other)
{
return getValue() + other.getValue();
}
}
Summable:
public interface Summable
{
public int add(Summable other);
public int getValue();
}
You implemented Summable interface in your City class, but forgot to include implements Summable in city class.
public class City implements Summable {
}

The constructor ClassCountry(String, String, String, int) is undefined

Eclipse keeps saying the constructor is undefined - what's wrong here? I have checked everything.
package exerciseOne;
public class TestCountryClass {
public static void main(String[] args) {
ClassCountry oCon1 = new ClassCountry("Iceland", "Icelandic", "króna", 400000);
System.out.printf("%s %s %d%n", oCon1.getCountryName(),
oCon1.getCountryLanguage(),
oCon1.getCountryCurrency(),
oCon1.getCountryPopulation());
}
}
Here is the code for the Class, I have checked multiple times but Eclipse kept returning the same error message. I hope you guys are able to find the issue here; any help is appreciated:
package exerciseOne;
public class ClassCountry {
private String countryName;
private String countryLanguage;
private String countryCurrency;
private int countryPopulation;
public void classCountry(String countryName, String countryLanguage, String countryCurrency, int countryPopulation)
{
this.countryName = countryName;
this.countryLanguage = countryLanguage;
this.countryCurrency = countryCurrency;
this.countryPopulation = countryPopulation;
}
public void setCountryName(String countryName)
{
this.countryName = countryName;
}
public String getCountryName()
{
return countryName;
}
public void setCountryLanguage(String countryLanguage)
{
this.countryLanguage = countryLanguage;
}
public String getCountryLanguage()
{
return countryLanguage;
}
public void setCountryPopulation(int countryPopulation)
{
this.countryPopulation = countryPopulation;
}
public int getCountryPopulation()
{
return countryPopulation;
}
public void setCountryCurrency(String countryCurrency)
{
this.countryCurrency = countryCurrency;
}
public String getCountryCurrency()
{
return countryCurrency;
}
}
public void classCountry(String countryName, String countryLanguage, String countryCurrency, int countryPopulation)
{
this.countryName = countryName;
this.countryLanguage = countryLanguage;
this.countryCurrency = countryCurrency;
this.countryPopulation = countryPopulation;
}
Here is your problem. You've added this as constructor, but this is not a constructor. This is a method with returntype void.
Change the above to:
public ClassCountry(String countryName, String countryLanguage, String countryCurrency, int countryPopulation)
{
this.countryName = countryName;
this.countryLanguage = countryLanguage;
this.countryCurrency = countryCurrency;
this.countryPopulation = countryPopulation;
}
For more information about constructors, you can check this Oracle tutorial.
Your "constructor" is not a constructor because it is a method.
Change classCountry to ClassCountry and remove the void.
public ClassCountry(...
package com.String;
public class StackOverFlow {
public static void main(String[] args) {
ClassCountry oCon1 = new ClassCountry("Iceland", "Icelandic", "króna", 400000);
System.out.printf("%s %s %s %d", oCon1.getCountryName(),
oCon1.getCountryLanguage(),
oCon1.getCountryCurrency(),
oCon1.getCountryPopulation());
}
private static class ClassCountry{
private String countryName;
private String countryLanguage;
private String countryCurrency;
private int countryPopulation;
public ClassCountry(String countryName, String countryLanguage, String countryCurrency, int countryPopulation) {
super();
this.countryName = countryName;
this.countryLanguage = countryLanguage;
this.countryCurrency = countryCurrency;
this.countryPopulation = countryPopulation;
}
public String getCountryName() {
return countryName;
}
public String getCountryLanguage() {
return countryLanguage;
}
public String getCountryCurrency() {
return countryCurrency;
}
public int getCountryPopulation() {
return countryPopulation;
}
}
}
I guess this will help
https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
The Class name is : ClassCountry.
The Constructor name should be same like : public ClassCountry ().
However in your case the constructor that you think it should be, is not actually a constructor, rather it is considered as a simple method.
So correct the spelling of the constructor name, also remove the void part.
The cause for this error message is the following line:
public void classCountry(...
A constructor must not have a return value, so you have to remove void. Also the constructor has to match the class definition with regard to upper and lower case. So change the line to
public ClassCountry(...
and it will work.
You have a class called ClassCountry so your constructor must take the form
public ClassCountry(args) {}
Java is a case sensitive language so classCountry will not match the constructor and you also had a void in your constructor which creates a method with no return value rather than a constructor.

Creating an object and calling it

this is my current code to store rooms(it compiles fine) but in the UML there is a variable called addEquipment and there is also another class called Equipment to be defined. I'm having trouble wrapping my head around what I'm supposed to do with this. Am I supposed to create and call an object called Equipment? what goes in addEquipment?
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private String equipmentList;
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public String getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(String anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
}
//Create room object
public Room(int capacity, String equipmentList) {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
return "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
}
}
You can create a new class Equipment and modify your attribute equipmentList to be a List:
public class Equipment {
private String name;
public Equipment(String name) {
this.name = name;
}
}
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private List<Equipment> equipmentList = new ArrayList<Equipment>();
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public List<Equipment> getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(List<Equipment> anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
Equipment oneEquipment = new Equipment(newEquipment);
equipmentList.add(oneEquipment);
}
//Create room object
public Room() {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
String capacity=String.valueOf(getCapacity());
String room = "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
return room;
}
}
In the method addEquipment, you can create a new Equipment and add it to equipmentList, like code above.
An Equipment class could be anything. Lets assume the "Equipment"-class has a String called "name" as it's attribute
public class Equipment {
String name;
public Equipment( String name) {
this.name = name;
}
public String getName() {
return this.name
}
}
When you extend your Room class by the requested "addEquipment" method, you can do something like this.
public class Room {
... // Your code
private int equipmentIndex = 0;
private Equipment[] equipment = new Equipment[10]; // hold 10 Equipment objects
public void addEquipment( Equipment eq ) {
if ( equipmentIndex < 10 ) {
equipment[ equipmentIndex ] = eq;
equipmentIndex++;
System.out.println("Added new equipment: " + eq.getName());
} else {
System.out.println("The equipment " + eq.getName() + " was not added (array is full)");
}
}
}
Now when you call
room.addEquipment( new Equipment("Chair") );
on your previously initialized object of the Room-class, you will get
"Added new equipment: Chair"
Hope this helps a bit.
PS: The code is untestet (maybe there hides a syntax error somewhere)

How to get an attribute/variable from a different class

I'm programming in a class and I need to have a variable from a different class. How can I do this?
package domein;
public class Speler
{
private String naam;
private String kleur;
private Sector sector;
private int Sector;
private int krediet = 10;
private int extraSchattingWaarde = 0;
private int nummer;
public Speler(String naam, String kleur, Sector sector)
{
setNaam(naam);
setKleur(kleur);
setSector(sector);
}
public String getNaam()
{
return this.naam;
}
public void setNaam(String naam)
{
//controle of het leeg is??
this.naam = naam;
}
public Sector getSector()
{
return this.sector;
}
private void setSector(Sector sector)
{
//tussen 1 en 4
this.sector = sector;
}
public String getKleur()
{
return this.kleur;
}
private void setKleur(String kleur)
{
//controle of het de beschikbare kleuren zijn
this.kleur = kleur;
}
public int getKrediet()
{
return this.krediet;
}
public void setKrediet(int krediet)
{
this.krediet = krediet;
}
public int getExtraSchattingWaarde()
{
return this.extraSchattingWaarde;
}
public void setExtraSchattingWaarde(int waarde)
{
this.extraSchattingWaarde = waarde;
}
}
This is the class where I need to get the variables and some methods. How can I make this class global?
Just a thing : this line is wrong private int Sector; because you can not use a class name as your variable name. This should hide your class visibility.
I assume this is an error and I continue the explanation.
In an other class you can instanciate this class and call the values. For example:
public class MyClass {
private Speler mySpeler = new Speler("AAA", "BBB", 3);
public MyClass() {}
public void myMethod() {
System.out.println(mySpeler.getKleur());
}
}

How to assign value to this function in java incompatible types

How to assign value to this function in Java incompatible types?
public class CustomerInfo implements Serializable {
private static final long serialVersionUID = 9083257536541L;
protected String id;
protected String searchkey;
protected String taxid;
protected String name;
protected String postal;
/** Creates a new instance of UserInfoBasic */
public CustomerInfo(String id) {
this.id = id;
this.searchkey = null;
this.taxid = null;
this.name = null;
this.postal = null;
}
public String getId() {
return id;
}
public String getTaxid() {
return taxid;
}
public void setTaxid(String taxid) {
this.taxid = taxid;
}
public String getSearchkey() {
return searchkey;
}
public void setSearchkey(String searchkey) {
this.searchkey = searchkey;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPostal() {
return postal;
}
public void setPostal(String postal) {
this.postal = postal;
}
public String printTaxid() {
return StringUtils.encodeXML(taxid);
}
public String printName() {
return StringUtils.encodeXML(name);
}
#Override
public String toString() {
return getName();
}
}
private CustomerInfo selectedCustomer;
public CustomerInfo getSelectedCustomer() {
// construct a CustomerInfo from the data in your String
return selectedCustomer;
}
private void jcmdOKActionPerformed(java.awt.event.ActionEvent evt) {
selectedCustomer = (CustomerInfo) jListCustomers.getSelectedValue();
//test code
String testing = m_jtxtName.getText();
System.out.println("Now the selectedCustomer is dispayed!");
System.out.println(selectedCustomer);
System.out.println(testing);
//test code
dispose();
}
In the above shown code, I need the string testing value to be assigned to selectedCustomer. How can I assign the value? This is the error I get:
selectedCustomer = m_jtxtName.getText();
incompatible types
required: CustomerInfo
found: String
You can't!!!
selectedCustomer is an object of type CustomerInfo.
m_jtxtName.getText() returns a String
You can't assign a String to a CustomerInfo.
Probably you need to do something like:
int id = 1; //Or whatever new id you have.
String name = m_jtxtName.getText();
selectedCustomer = new CustomerInfo(name); //or whatever id you have.
selectedCustomer.setName(name); //or whatever name you have.
EDIT:
Something is missing from your class. Either it needs setter methods (it has only getters now, so you can't set other properties as name etc) or it needs a constructor with four arguments like:
public CustomerInfo(String id, String searchKey, String taxid, String name, String postal) {
this.id = id;
this.searchKey = searchKey;
// etc
In this case, you might have six jtextfields in your screen, so te user can fill all fields and the create the Customerinfo object by passing all parameters to the constructor.
you cannot do it by simply casting a String to a CustomerInfo object, but you could extend your CustomerInfo but you could try something like this:
public class CustomerInfo {
[...]
public static CustomerInfo createCustomerInfo(String data) {
// construct a CustomerInfo from the data in your String
return createdCustomerInfo;
}
}
I don't know what data you have in that String so i can not give you an advice how to implement this. e.g. If it is the ID you could use this to retrieve the CustomerInfo from database or something like that.

Categories

Resources