#Java Assignment
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
so this is almost too easy of a question for us here
I want to get a sense of what you do understand at this point before responding
so can you share your assignment/what you've done so far?
Just started this coding class towards my electrical engineering major. Teacher basically just had us subscribe to a course that has a bunch of assignments attached to it and said "do all these for your grades" so Im just going through some online course that doesnt give me too much detailed information. And when I get things wrong. It just says error. Im constantly finding myself watching youtube videos for all these things that seem so, as you said, easy. So far from youtube soley, I understand creating public classes, integers, and printing texts. Also giving things like set names.
yeah so okay
lets start small here
class Thing {
}
a class declaration looks like this
idk thats kinda a lot but Im honestly thinking of switching teachers if I can, I really feel like im just being given questions without any help and constantly watching youtube to help haha. But I thought I mine as well ask someone in this server for small help
it is declaring a new "classification" of objects
oh ok
if objects of this classification hold data
then we need to put that data in "fields"
easy example
class Dog {
String name;
}
all dogs have names
so the dog class would need to contain a field that can hold the name
Dog dog = new Dog();
dog.name = "Fido";
so dogs are "boxes" that can hold a name
pausing there for questions
oh yeah, this part is basically what I already know, from the video I watched. But basically nothing further, Kinda
ok
is "Fido" an acceptable name for a dog?
I would think so
okay, good
and we can make a dog named fido like this
Dog dog = new Dog();
dog.name = "Fido";
now
is "Sally" an acceptable name for a dog?
I guess so too but I wouldnt name a dog that haha
that was one of my dogs names so you take that shit back
you can make a dog named sally like this
Dog dog = new Dog();
dog.name = "Sally";
haha sorry sorry
okay now
oh ok
is "" an acceptable name for a dog?
no
what would that do
just make a dog whose name is an empty string
we don't want that because we said "all dogs have a name"
yeah
and real quick, did you learn about null?
no
ok haha
so thats the "problem" with fields
you can always set them to anything that is valid for the type
oh ok
if you have a String field, you can set it to any String
I know what a String is but there is a more like specific definition to it. Like I know when to put it because I saw others do it but not necessarily the purpose
Strings are text
oh ok thats what I assumed but just needed to make sure
Book teaching how to write modern and effective Java. It is maintained by the community, anyone can contribute.
so when it comes to public and private classes. what is the difference
or point of using one or the other
well lets stick to fields for now
ok
if you have a field declared like this
class Dog {
String name;
}
we call that "package-private"
that means that any code in "the same package" can access it
but code outside that package cannot
all java classes live in some package
like java.lang.String
the String class lives in the java.lang package
so if you tried printing text outside of it, you counn't pull the string name from that class? (if my question makes sense)
nah you can still access it from outside the class
just not outside the package
honestly, its hard to explain in full
but for all intents and purposes
so vice versa wouldnt work
class Dog {
public String name;
}
class Dog {
String name;
}
you can kindof think of these as "the same" while you are writing basic programs
oh ok
the whole public/private thing is useful for
- "object design"
- the design of larger programs
but okay
back to private
ok
one way to restrict people's ability to set a variable is to mark it private
class Dog {
private String name;
}
ohh
this means that no code outside of the Dog.java file can set the field's value
or read its value
so if we wanted, now we can make restrictions like "dog names cannot be an empty string"
so what I asked before, would that be true here with a private field? Outisde not accessing inside. Or are you getting at something else
class Dog {
private String name;
void giveName(String newName) {
if (newName.equals("")) {
// crash
}
else {
name = newName;
}
}
}
so if you tried printing text outside of it, you counn't pull the string name from that class? (if my question makes sense)
If you mean this
then yes
yes
with a private field you cannot pull the string name from the class
unless you are inside Dog.java
ohh ok ok
now, we answer a lot of student questions here, so my spider sense is tuned
so I can guess what your curriculum is going to try to show you next
(I think I gotta switch professors, talking to a random guy on discord has been more helpfull in passed couple minutes then the passed 2 hours I spent reading these sentences he sent us over and over again haha)
and what is that?
getters and setters
its...nonsense
obviously if you don't care about code just cheat and get the grade, fuck with circuits for the rest of your life or whatever
but otherwise, we generally point people here
Helsingin yliopiston kaikille avoin ja ilmainen ohjelmoinnin perusteet opettava verkkokurssi. Kurssilla perehdytään nykyaikaisen ohjelmoinnin perusideoihin sekä ohjelmoinnissa käytettävien työvälineiden lisäksi algoritmien laatimiseen. Kurssille osallistuminen ei vaadi ennakkotietoja ohjelmoinnista.
try going through this course to learn and answer questions on your assignments after
a random guy on discord
TBF you hit the jackpot
and i'm humble too
I do care about coding and honestly find it super interesting. So Im trying to strain away from cheating and trying to understand what Im actually tying and doing so this helps alot
thank you lots man
or woman
but thanks alot
is this a course I have to pay for?
thats a dumb question ill just look hasha
no it is not
Its free
make other question threads for that
for what
people who are not me have gone through it here and can help you get set up
for asking more in depth questions about the mooc.fi course
oh ok
just keeps it tidy
one more question, I think I know the answer to it but not too sure
oh actually nvm I think I got it
k
so defining a private field in a public class... inside the public class name, I write another line and just name it private before right? I need to do so with an integer
with how new I am, I dont know if my questions 100 percent make sense so if they dont then ill try and reword if I can
u weite it in the class body which is inbetween the curly brace pair right after the class name