Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Keep getting the error, I can't seem to figure out why, the abstract method is implemented. I tried adding the keyword public, but that didn't help at all. I've read examples on the internet about abstract method, but mostl of them are void.
The abstract class:
public abstract class Osoba {
private String ime_prezime;
private char pol;
public Osoba(String ime_prezime, char pol) {
this.ime_prezime=ime_prezime;
this.pol=pol;
}
public String getImePrezime() { return ime_prezime; }
public void setImePrezime(String ime_prezime) { this.ime_prezime=ime_prezime; }
public char getPol() { return pol; }
public void setPol(char pol) { this.pol=pol; }
abstract int brojGodinaOsobe(Datum danasnji_datum);
}
And the subclass:
public class OsobaDatum extends Osoba{
private Datum datum_rodjenja;
public OsobaDatum(String ime_prezime, char pol, final Datum datum_rodjenja) {
super(ime_prezime, pol);
this.datum_rodjenja=datum_rodjenja;
}
public String toString() {
return "OsobaDatum: \n" + "Ime i prezime: " + getImePrezime() +
"\n" + "Pol: " + getPol() + "\nDatum rodjenja: "
+ datum_rodjenja.toString();
}
int brojGodina(Datum danasnji_datum) {
if ( datum_rodjenja.getMesec() < danasnji_datum.getMesec() ) {
return danasnji_datum.getGodina() - datum_rodjenja.getGodina();
}
else if (datum_rodjenja.getDan() <= danasnji_datum.getDan() &&
datum_rodjenja.getMesec() == danasnji_datum.getMesec()) {
return danasnji_datum.getGodina() - datum_rodjenja.getGodina();
}
else
return danasnji_datum.getGodina() - datum_rodjenja.getGodina() -1 ;
}
}
The abstract method's name is brojGodinaOsobe() and it looks like you only implemented a method called brojGodina(). Add the Osobe and you should be OK.
It looks like you are not overriding the method brojGodinaOsobe(Datum danasnji_datum); from Super class.
Change brojGodina(Datum danasnji_datum) to brojGodinaOsobe(Datum danasnji_datum) in the OsobaDatum class.
Regards.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I am trying to calculate the print cost base on different paper size, single side or double side. So here is the detail:
Also need to support for other paper sizes will be added in the future.
And according to my design, developer can just create a A5 class for example to support other paper size, and add other condition in the factory class.
Could someone review my code and help me on whether I have to use interface instead of abstract class?
Here is my code:
PageBase:
public abstract class PageBase {
abstract double GetCost(int total, int color, boolean isSingleSide);
abstract void CalculateUnitPrice(boolean isSingleSide);
}
A4Page Class:
public class A4Page extends PageBase {
public double blackAndWhitePrintUnitCost;
public double colorPrintUniCost;
#Override
public double GetCost(int total, int color, boolean isSingleSide) {
CalculateUnitPrice(isSingleSide);
return color* colorPrintUniCost + (total-color)* blackAndWhitePrintUnitCost;
}
#Override
public void CalculateUnitPrice(boolean isSingleSide) {
if (isSingleSide) {
this.blackAndWhitePrintUnitCost = 0.15;
this.colorPrintUniCost = 0.25;
}
else {
this.blackAndWhitePrintUnitCost = 0.10;
this.colorPrintUniCost = 0.20;
}
}
}
PageFactory:
public class PageFactory {
public PageBase GetPage(String pageType) {
switch (pageType.toUpperCase()) {
case "A4":
return new A4Page();
default:
return new A4Page();
}
}
}
Main:
public class Main {
public static void Main() {
//read
PageFactory pageFactory = new PageFactory();
PageBase page = pageFactory.GetPage("A4");
page.GetCost(0,0,false);
}
}
Decorator is way more elegant than Factory to your problem.
For Decorator, you will need some classes and interfaces:
Interfaces: Colored, Side and Page. All interfaces has a method cost() to be implemented.
Classes: SingleSide, DoubleSide, ColorPage, BlankAndWhitePage, A4
Usage:
Page page1 = new A4(new SingleSide(new ColorPage()))
Page page2 = new A4(new DoubleSide(new BlankAndWhitePage()))
page1.cost();
You need to add some value to each component, to be summed and give the expected value. Each object has a "cost".
Some internals:
class A4 implements Page {
//constructor
private Side side;
public BigDecimal cost() {
return this.valueA4 + side.cost();
}
}
class SingleSide implements Side {
//constructor
private Colored colored;
public BigDecimal cost() {
return this.valueSingleSided+ colored.cost();
}
}
Something in this line could give you some insights about the best object organization.
The following code is the solution code, which declares an abstract class with one absract subclass (that in turn has two subclasses) and another subclass. Because the method setScore() has to be implemented in three different ways, and the objecttype differs for each case, I'm presuming we use the parameter P to define it.
public abstract class Vak<P> implements EvaluatieSpecificatie,
Comparable<Vak> {
private VakInfo vak;
public abstract void setScore(P parameter);
public Vak(VakInfo v) {
vak = v;
}
public String getVakNaam() {
return vak.getNaam();
}
public int getStudiepunten() {
return vak.getStudiepunten();
}
public String getVakcode() {
return vak.getVakcode();
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(vak);
if (isGeslaagd()) {
sb.append(" C");
} else {
sb.append(" niet geslaagd - moet hernomen worden");
}
return sb.toString();
}
#Override
public int compareTo(Vak v) {
// sorteren van vakken op basis van studiepunten
// this < v --> neg waarde teruggeven
// this > v --> pos waarde teruggeven
// this = v --> 0 teruggeven
return this.getStudiepunten() - v.getStudiepunten();
}
}
Now, what I don't understand is how exactly this class ClassName"<"Parameter">" thing works, when to use it, or what exactly its called. I've tried to look it up, but I can't seem to find any information on it. Could anyone explain to me what exactly this is, and how/when it's supposed to be used? (Or link me in the right direction), perhaps by giving a simple example (since I'm not sure if asking for code is required).
I'm terribly sorry if this question isn't appropriate, but I'd really like to understand, so here goes.
Appereantly it's called a generic type. Thank you #HRgiger.
https://docs.oracle.com/javase/tutorial/java/generics/types.html
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a quick question
Suppose I were to create a data definition class named Campaign
and then I want to make an implementation class
Let's say in this implementation class I have a few variables(campaignName, campaignType, campaignGoal) and some methods (getCampName, getCampType, getCampGoal).
I want to create two different campaigns classes, and then compare their information at the end of the program with a print method.
Would this be a proper way of declaring information for the first campaign:
Campaign campaign1 = new Campaign();
which is in the main method, and then let's say I want to get the name of the first campaign, which is just hardcoded for now
public static String campaign1.getCampName(){
campaign1.setCampName("IT Student Scholarship Fund");
}
I just wanted to know how to do this. Thank you.
getCampName() should look something like:
public String getCampName() { return this.campaignName; }
a then simply
campaign1.getName();
You should stop the practice of putting all of your code in a main method. Instead, instantiate your Campaign instances and call methods on each one using a driver method in your primary class. In addition, you can override the equals() method to do the comparison (or implement Comparator).
public class CampaignTest{
public void runTest(){
Campaign c1 = new Campaign("First Campaign");
Campaign c2 = new Campaign("Second Campaign");
Campaign c11 = new Campaign("First Campaign");
System.out.println("c1==c2: " + c1.equals(c2));
System.out.println("c2==c11: " + c2.equals(c11));
System.out.println("c1==c11: " + c1.equals(c11));
}
public static void main(String... args){
CampaignTest test = new CampaignTest();
test.runTest();
}
private class Campaign{
private String name;
public Campaign(String n){
this.name = n;
}
public String getName(){
return name;
}
#Override
public boolean equals(Object other){
if(other instanceof Campaign && ((Campaign)other).getName().equals(name)){
return true;
}
return false;
}
}
}
This question already has answers here:
What causes "'void' type not allowed here" error
(7 answers)
Closed 10 months ago.
I'm learning to use classes and part of my assignment is to make this Car class. I'm getting an error on line 6 where I attempt to print of the results of the methods within the class. I think this means that I'm attempting to print something that doesn't exist and I suspect it's the mileage method. I tried changing it to return miles, but that didn't work either. Any ideas?
public class TestCar {
public static final void main(String args[]) {
Car c = new Car ();
c.moveForward(4);
System.out.println ("The car went" + c.mileage() + "miles."); // <-- L6
}
}
class Car {
public int miles = 2000;
public void moveForward(int mf) {
if (miles != 2000) {
miles += mf;
}
}
public void mileage() {
System.out.print(miles);
}
}
The error message is telling you exactly what is wrong -- you're trying to extract a result from a method that does not return a result.
Instead, have the mileage() method return a String, not print out a String.
public String mileage() {
return String.valueOf(miles);
}
Myself, I'd make this a getter method, and instead would do:
public int getMiles() {
return miles;
}
Car.mileage() is void, i.e., does not return anything. It needs to return something, like in:
public int mileage() {
return miles;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have ArrayList and protected getter function in first class and function in second class, which extends first class. I do getList().size() in function, but getting 0. If I do myList.size() in first class, I getting right answer.
Code:
protected List<WrappedGameProfile> getList(){
return Text;
}
public int getServerInfoSize(){
return super.getList().size(); //returns 0
}
All code:
package ServerInfo;
//imports
public class ServerInfo extends JavaPlugin {
Logger log;
private List<WrappedGameProfile> Text = new ArrayList<WrappedGameProfile>();
FileConfiguration config;
protected String ConvertFormat(String format){
return format.replace("#0", ""+ChatColor.BLACK).replace("#1", ""+ChatColor.DARK_BLUE).replace("#2", ""+ChatColor.DARK_GREEN).replace("#3", ""+ChatColor.DARK_AQUA).replace("#4", ""+ChatColor.DARK_RED).replace("#5", ""+ChatColor.DARK_PURPLE).replace("#6", ""+ChatColor.GOLD).replace("#7", ""+ChatColor.GRAY).replace("#8", ""+ChatColor.DARK_GRAY).replace("#9", ""+ChatColor.BLUE).replace("#a", ""+ChatColor.GREEN).replace("#b", ""+ChatColor.AQUA).replace("#c", ""+ChatColor.RED).replace("#d", ""+ChatColor.LIGHT_PURPLE).replace("#e", ""+ChatColor.YELLOW).replace("#f", ""+ChatColor.WHITE);
}
protected List<WrappedGameProfile> getList(){
return Text;
}
protected void setText(List<WrappedGameProfile> Text){
this.Text = Text;
}
public void onEnable(){
log = getLogger();
log.info("GuiServerInfo activating...");
this.saveDefaultConfig();
config = getConfig();
ProtocolLibrary.getProtocolManager().addPacketListener(
new PacketAdapter(this, ListenerPriority.NORMAL,
Arrays.asList(PacketType.Status.Server.OUT_SERVER_INFO), ListenerOptions.ASYNC) {
#Override
public void onPacketSending(PacketEvent event) {
handlePing(event.getPacket().getServerPings().read(0));
}
}
);
for (int i = 0; i < config.getStringList("Text").size();i++){
Text.add(
new WrappedGameProfile(
"id" + i + 1,
ConvertFormat(config.getStringList("Text").get(i)))
);
}
log.info("GuiServerInfo active!");
}
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender.isOp()){
if (args.length > 0){
if (args[0].equalsIgnoreCase("set")){
Text.clear();
List<String> newConf = new ArrayList<String>();
for (int i = 1; i < args.length; i++){
Text.add(new WrappedGameProfile("id" + Text.size() + 1, ConvertFormat(args[i])));
newConf.add(args[i]);
log.info("Add ServerInfo: " + args[i]);
}
config.set("Text", newConf);
sender.sendMessage("Successful!");
return true;
}
}
return false;
} else {
sender.sendMessage("You not op!");
return true;
}
}
private void handlePing(WrappedServerPing ping) {
ping.setPlayers(Text);
}
public void onDisable(){
saveConfig();
log.info("GuiServerInfo disabled!");
}}
And:
package ServerInfo;
import java.util.ArrayList;
import java.util.List;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
public class ServerInfoAPI extends ServerInfo {
public void setServerInfo(List<WrappedGameProfile> Text){
super.setText(Text);
super.log.info("Set new ServerInfo");
}
public void setServerInfo(String[] Text){
List<WrappedGameProfile> tmp = new ArrayList<WrappedGameProfile>();
for (int i = 0; i < Text.length; i++) {
tmp.set(i, new WrappedGameProfile("id" + i + 1, ConvertFormat(Text[i])));
}
super.setText(tmp);
super.log.info("Set new ServerInfo");
}
public void insertServerInfoString(String str, int num){
getList().set(num, new WrappedGameProfile("id" + num, ConvertFormat(str)));
super.log.info("Add new ServerInfo - num:" + num + ", Text:" + str);
}
public void addServerInfoString(String str){
super.getList().add(new WrappedGameProfile("id" + super.getList().size() + 1, ConvertFormat(str)));
super.log.info("Add new ServerInfo: " + str);
}
public int getServerInfoSize(){
return super.getList().size();
}
public String getServerInfoString(int num){
return super.getList().get(num).getName();
}
public int getServerInfoStringNum(WrappedGameProfile pr){
return super.getList().indexOf(pr);
}
}
Just your terminology alone shows a serious lack of understanding of things like Objects, Classes, Packages, etc. you need to read up on some introductory tutorials.
To answer the question it sounds like you have created a new instance inheriting from a super class. Because it is a new instance the list is not shared, so changes you make in one class do not get seen.
To share one list between multiple objects you need to pass the list object from one to another.
Don't try to globalize variables. Instead call public methods of the class that holds the variable that will allow other objects to query its state or make controlled change to its state.
I agree also with the others: it sounds as if you're misusing inheritance, since this is not a problem that should be solved with inheritance, but only your code (which we haven't seen at the time of this writing) will tell us for sure.
Regarding:
No, i have just one List
No, if you're using inheritance, then you have more than one List. Again don't try to solve this with inheritance.
Why don't you have a
private List<WrappedGameProfile> text = new ArrayList<WrappedGameProfile>();
public List<WrappedGameProfile> getText() {
return text;
}
in your super class?
than your code will be :
public int getServerInfoSize(){
return getText().size();
}
and please do camelcasing correct, the variable Text has to be text.