Can anyone help me with my Java homework?

Can anyone help me with my Java homework?

It's on enums and for this website we're told not to use the first "public" for the class declaration.

Question in pic - my attempt is here:

class SerialPublication {

public enum Frequency {DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY, UNDEFINED}

protected Frequency frequency;

public SerialPublication(){
frequency = Frequency.UNDEFINED;
}

public SerialPublication(String freq){
this.setFrequency(freq);
}

public void setFrequency(freq){
frequency = Frequency.freq;
}

public Frequency getFrequency(){
return frequency;
}
}

This is the error message I got

bump

bumping

>public void setFrequency(freq)
Don't do Java but shouldn't you be declaring what type of variable "freq" is?

>public void setFrequency(String freq)

this.

POO IN LOO PAJEET BACK TO LE REDDITZ XDDDDDD

You were missing the type in the setter. And I wouldn't bother with the constructor setting the property default, just assign it like I've done

public class SerialPublication {

public enum Frequency {DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY, UNDEFINED}

private Frequency frequency = Frequency.UNDEFINED;

public SerialPublication(String freq){
this.setFrequency(Frequency.valueOf(freq));
}

public void setFrequency(Frequency freq) {
frequency = freq;
}

public Frequency getFrequency(){
return frequency;
}
}

Oh and I forgot, use Frequency.valueOf to convert from a string to the enum. But beware this should have error handling to handle non-matching strings.

Read the brief, the OP had a solution which would have got better marks than yours.

This some high level techno babel up in here.

yes yes, but I didn't look at his brief just the code..

This is probably more what you want, cba to check, too bloody tired..

class SerialPublication {

public enum Frequency {DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY, UNDEFINED}

protected Frequency frequency;

public SerialPublication() {
this.frequency = Frequency.UNDEFINED;
}

public SerialPublication(String freq) {
try {
this.frequency = Frequency.valueOf(freq);
} catch (IllegalArgumentException ex) {
this.frequency = Frequency.UNDEFINED;
}
}

public void setFrequency(Frequency freq) {
this.frequency = freq;
}

public String getFrequency(){
return this.frequency.name().toLowerCase();
}

public static void main(String [] args) {
SerialPublication sp = new SerialPublication("foo");
System.out.println(sp.getFrequency());
}
}

OP here. You're right guys, dumb mistake - my attempt timed out but this was my updated code and new error message:

class SerialPublication {

public enum Frequency {DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY, UNDEFINED}

protected Frequency frequency;

public SerialPublication(){
frequency = Frequency.UNDEFINED;
}

public SerialPublication(String freq){
setFrequency(freq);
}

public void setFrequency(Frequency freq){
frequency = freq;
}

public Frequency getFrequency(){
return frequency;
}
}

gfdgfdsgdfs

SerialPublication passes String freq to setFrequency which expects a Frequency

OHHH

thank you so much!

I've worked with a lot of students who are learning coding at it seems that none of them do it as a hobby and therefore never grasp the basics.

As an oldfag, I grew up with no access to computers until a friend of my mom leant me a ZX81 (Yes I am that old!) I taught myself to code BASIC and over the years have mastered everything from Assembly, C++ to Swift.

With some basic ground knowledge and a willingness to code at home for fun, there is no limit to what can be achieved. However if you just want to code to earn good money, you _will_ fail.

I'm gonna keep posting questions that may be really stupid here for anyone that might stick around. Thanks for the help on the previous q!

I agree I want to learn how to code myself and I do enjoy it but it's always so much easier doing it of my own accord VS learning how to do it because an assignment is due really soon.

In this case where I'm under some pressure to learn it just clicks a lot slower for me

This one is to do with inheritance and it looks like a really short answer.

This is just one of those where I don't know syntax or what to do because I haven't gotten my head around inheritance.

bump

this one looks like a simple one

Use the "this" operator when instantiating the vegetable and weightinkg class variables, just like the provided class in that example.

this.colour = colour;
this.weightInKg = weightInKg;

...

Update: Got it - all I had to do was use this.colour and this.weightInKg

Oh yeah thanks heaps! I was just experimenting and got it eventually.