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

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.

Related

how to convert string into Json and extrat from it info

I'm using retrofit2 and Rxjava2 to insert/get information from mongodb and nodeJs server, for now, I receive all data as a string but I want to get hole collection Infos from my base so I need to convert string to JSON and get each information.
My code to receive data:
1- Service:
#POST("collect/get")
#FormUrlEncoded
Observable<String> getcollection(#Field("selector") String selector);
2-RetrofitClient:
if(instance == null){
instance = new Retrofit.Builder()
.baseUrl("http://transportor.ddns.net:3000/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create()).build();
}
3- Recieve function
private void getallcollection(String selector) {
compositeDisposable.add(myServices.getcollection(selector)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>(){
#Override
public void accept(String s) throws Exception {
Log.d("infos",s);
}
}));
}
I'm already prepared Collection class:
public class col {
private String creator;
private String emailcol;
private String date_creation_col;
private String nom_col;
private String long_col;
private String lat_col;
private String tel_fix_col;
private String tel_mobile_col;
private String creatorcreator;
private String heure_matin_col;
private String heure_apresmatin_col;
private String type;
private String imagePath;
public col(String creator, String emailcol, String date_creation_col, String nom_col, String long_col, String lat_col, String tel_fix_col, String tel_mobile_col, String creatorcreator, String heure_matin_col, String heure_apresmatin_col, String type, String imagePath) {
this.creator = creator;
this.emailcol = emailcol;
this.date_creation_col = date_creation_col;
this.nom_col = nom_col;
this.long_col = long_col;
this.lat_col = lat_col;
this.tel_fix_col = tel_fix_col;
this.tel_mobile_col = tel_mobile_col;
this.creatorcreator = creatorcreator;
this.heure_matin_col = heure_matin_col;
this.heure_apresmatin_col = heure_apresmatin_col;
this.type = type;
this.imagePath = imagePath;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getEmailcol() {
return emailcol;
}
public void setEmailcol(String emailcol) {
this.emailcol = emailcol;
}
public String getDate_creation_col() {
return date_creation_col;
}
public void setDate_creation_col(String date_creation_col) {
this.date_creation_col = date_creation_col;
}
public String getNom_col() {
return nom_col;
}
public void setNom_col(String nom_col) {
this.nom_col = nom_col;
}
public String getLong_col() {
return long_col;
}
public void setLong_col(String long_col) {
this.long_col = long_col;
}
public String getLat_col() {
return lat_col;
}
public void setLat_col(String lat_col) {
this.lat_col = lat_col;
}
public String getTel_fix_col() {
return tel_fix_col;
}
public void setTel_fix_col(String tel_fix_col) {
this.tel_fix_col = tel_fix_col;
}
public String getTel_mobile_col() {
return tel_mobile_col;
}
public void setTel_mobile_col(String tel_mobile_col) {
this.tel_mobile_col = tel_mobile_col;
}
public String getCreatorcreator() {
return creatorcreator;
}
public void setCreatorcreator(String creatorcreator) {
this.creatorcreator = creatorcreator;
}
public String getHeure_matin_col() {
return heure_matin_col;
}
public void setHeure_matin_col(String heure_matin_col) {
this.heure_matin_col = heure_matin_col;
}
public String getHeure_apresmatin_col() {
return heure_apresmatin_col;
}
public void setHeure_apresmatin_col(String heure_apresmatin_col) {
this.heure_apresmatin_col = heure_apresmatin_col;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
}
Actually I received all data and console show me : [{"_id":"5e22074673c926147c3a73f5","date_creation_col":"17-01-2020","creator":"Alaeddine","emailcol":"amir#gmail.com","nom_col":"amir","long_col":"10.179326869547367","lat_col":"36.83353893150942","tel_fix_col":"123","tel_mobile_col":"1234","adress_col":"rue Paris mision 34","heure_matin_col":"7","heure_apresmatin_col":"5","type":"collection","imagePath":"mmmmmmmmmmmm"}]
I want to know how to extract for example creator from this Json.
You can use a third-party JSON parser, like Google GSON, as you're already developing for Android. Java does not seem to contain a built-in JSON parser.
See this answer.

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

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.

How to sort object array in Java [duplicate]

This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
Closed 6 years ago.
I have an object List with the name of cars and in this list I have some vehicle object. all objects values are different to each other. In all object here is custom field with the name of unit and in unit is another field with the name of unitType ... which is "T20" , "VT11" or any other ..
I want to sort this cars List according to unitType like:
if cars(0).getUnit().getUnitType().equals("T20") sort all the value first then other "VT11" and then all other...
How can I do this?? Is any suitable method in java?
my Unit class:
package com.vehicletracking.vtss.classes.bean;
import java.io.*;
public class Unit implements Serializable{
private int code;
private int unitId;
private String unitType; //X8, X1, X1+, VT10, VT200, SPT100
private Sim sim;
private String commType; //CSD, SMS, GPRS, AUTO
private int modemCode;
private long IMEI;
private String serialNo;
private InputReportProfile inputReportProfile;
private String firmware;
private String packageName;
private String password;
public Unit() {
}
public Unit(int unitId) {
this.unitId = unitId;
}
public Unit(int unitId, String unitType) {
this.unitId = unitId;
this.unitType = unitType;
}
public Unit(int unitId, String unitType, Sim sim) {
this.unitId = unitId;
this.unitType = unitType;
this.sim = sim;
}
public void setUnitId(int unitId) {
this.unitId = unitId;
}
public int getUnitId() {
return this.unitId;
}
public void setUnitType(String unitType) {
this.unitType = unitType;
}
public String getUnitType() {
return this.unitType;
}
public void setSim(Sim sim) {
this.sim = sim;
}
public void setCommType(String commType) {
this.commType = commType;
}
public void setModemCode(int modemCode) {
this.modemCode = modemCode;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
public void setCode(int code) {
this.code = code;
}
public void setInputReportProfile(InputReportProfile inputReportProfile) {
this.inputReportProfile = inputReportProfile;
}
public void setPassword(String password) {
this.password = password;
}
public void setIMEI(long IMEI) {
this.IMEI = IMEI;
}
public Sim getSim() {
return this.sim;
}
public String getCommType() {
return commType;
}
public int getModemCode() {
return modemCode;
}
public String getSerialNo() {
return serialNo;
}
public int getCode() {
return code;
}
public InputReportProfile getInputReportProfile() {
return inputReportProfile;
}
public String getPassword() {
return password;
}
public long getIMEI() {
return IMEI;
}
public int hashCode() {
return unitId;
}
public boolean equals(Object obj) {
if (obj instanceof Unit) {
return this.unitId == ((Unit) obj).getUnitId();
}
return false;
}
public String toString() {
return this.unitId + "/" + this.unitType;
}
public String getFirmware() {
return firmware;
}
public void setFirmware(String firmware) {
this.firmware= firmware;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
}
Car List:
List cars;
From Java 8 Documentation for Comparable:
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.
According to above, you just have to implement Comparable, and provide an implementation for compareTo for your object. Collections.sort takes care of the rest.

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 can I pass a parameter into a constructor of type of a class I have created?

For example:
I have created a Publisher class and I pass into a constructor.
public Book(String pulicationTitle, Date publicationDate, Publisher publisher, String ISBN_10, String ISBN_13, String authorName) {
super(pulicationTitle, publicationDate, publisher);
ISBN_10 = ISBN_10;
ISBN_13 = ISBN_13;
this.authorName = authorName;
bookCounter++;
}
And then in my testing class in which I excute:
Book book2 = new Book(pulicationTitle, publicationDate, publisher, ISBN_10, ISBN_13, authorName);
In my Publisher class there is a method called getPublisherId() and in my testing class I have to pass that id.
This what I did in my testing class:
Date date = new Date();
Publisher pub = new Publisher("Cenage Learning", "13344-Tobean Drive,Independence, KY 45536", "Josef Blumenfield");
Book book = new Book("The world is Flat", date,pub.getPublisherID(), "037889948837", "099887636627", "Thomas L. Friedman");
And it is giving me an error
My Publisher class:
import java.util.ArrayList;
public class Publisher {
static int publisherCounter;
protected static int publisherID;
protected static String publisherName;
private String publisherAddress;
private String contactName;
private ArrayList publications;
public Publisher(String publisherName, String publisherAddress,
String contactName) {
super();
this.publisherName = publisherName;
this.publisherAddress = publisherAddress;
this.contactName = contactName;
publisherCounter++;
publisherID = publisherCounter;
}
public static String getPublisherName() {
return publisherName;
}
public void setPublisherName(String publisherName) {
this.publisherName = publisherName;
}
public String getPublisherAddress() {
return publisherAddress;
}
public void setPublisherAddress(String publisherAddress) {
this.publisherAddress = publisherAddress;
}
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public String printPublisherDetails(){
return publisherID+publisherName+publisherAddress+contactName;
}
public String printAllPublications(){
return Publication.getPulicationTitle() + Journal.getArticleTitle();
}
public static int getPublisherID() {
return publisherID;
}
public ArrayList getPublications() {
return publications;
}
public void addPublication(String adds){
publications.add(adds);
}
}
Your Book constructor accepts an Publisher object and you are passing the publisher ID, either change your Book constructor or overload it :-
public Book(String pulicationTitle, Date publicationDate, int publisherId, String ISBN_10, String ISBN_13, String authorName) {
super(pulicationTitle, publicationDate, publisherId);
ISBN_10 = ISBN_10;
ISBN_13 = ISBN_13;
this.authorName = authorName;
bookCounter++;
}
Also change or overload publisher constructor since it accepts different parameters:-
public Publisher(String publicationTitle,Date publicationDate,int publicationId){
}
There are various problems in the code.The below modifications can solve some part of your code
Book class constructor is expecting Publisher object in your code but you are passing primitive type
Add the code below
You can have overloaded constructor
public Book(String pulicationTitle, Date publicationDate, String ISBN_10, String ISBN_13, String authorName) {
//operations
}
You can have a method in Book class like this pass the id expicitly using method call
public void getpubId(int pubId)
{
// use pubId
}
call the method from your testing class like this
//create book object
Book book = new Book("The world is Flat", date, "037889948837", "099887636627", "Thomas L. Friedman");
//instead of passing publisher id through constructor, pass it through a method call
book.getpubId(publisher.getId());

Categories

Resources