#Java Assignment

1 messages · Page 1 of 1 (latest)

pine tide
#

I am being asked to declare two things named "integer fee" and "integer year" as private fields. What does that mean to delacire as private feilds? and how would I go about that.

pine kelpBOT
#

<@&987246399047479336> please have a look, thanks.

pine kelpBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

stark edge
#

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?

pine tide
#

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.

stark edge
#

yeah so okay

#

lets start small here

#
class Thing {

}
#

a class declaration looks like this

pine tide
#

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

stark edge
#

it is declaring a new "classification" of objects

pine tide
#

oh ok

stark edge
#

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

pine tide
#

oh yeah, this part is basically what I already know, from the video I watched. But basically nothing further, Kinda

stark edge
#

gotcha

#

so lets go through a hypothetical

pine tide
#

ok

stark edge
#

is "Fido" an acceptable name for a dog?

pine tide
#

I would think so

stark edge
#

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?

pine tide
#

I guess so too but I wouldnt name a dog that haha

stark edge
#

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";
pine tide
#

haha sorry sorry

stark edge
#

okay now

pine tide
#

oh ok

stark edge
#

is "" an acceptable name for a dog?

pine tide
#

no

stark edge
#

but we can make a dog with no name like this

#
Dog dog = new Dog();
dog.name = "";
pine tide
#

what would that do

stark edge
#

just make a dog whose name is an empty string

#

we don't want that because we said "all dogs have a name"

pine tide
#

yeah

stark edge
#

and real quick, did you learn about null?

pine tide
#

no

stark edge
#

okay then we will ignore it

#

for now

pine tide
#

ok haha

stark edge
#

so thats the "problem" with fields

#

you can always set them to anything that is valid for the type

pine tide
#

oh ok

stark edge
#

if you have a String field, you can set it to any String

pine tide
#

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

stark edge
#

Strings are text

pine tide
#

oh ok thats what I assumed but just needed to make sure

stark edge
pine tide
#

so when it comes to public and private classes. what is the difference

#

or point of using one or the other

stark edge
#

well lets stick to fields for now

pine tide
#

ok

stark edge
#

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

pine tide
#

so if you tried printing text outside of it, you counn't pull the string name from that class? (if my question makes sense)

stark edge
#

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

pine tide
#

so vice versa wouldnt work

stark edge
#
class Dog {
    public String name;
}
class Dog {
    String name;
}
#

you can kindof think of these as "the same" while you are writing basic programs

pine tide
#

oh ok

stark edge
#

the whole public/private thing is useful for

  1. "object design"
  2. the design of larger programs
#

but okay

#

back to private

pine tide
#

ok

stark edge
#

one way to restrict people's ability to set a variable is to mark it private

#
class Dog {
    private String name;
}
pine tide
#

ohh

stark edge
#

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"

pine tide
#

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

stark edge
#
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

pine tide
#

yes

stark edge
#

with a private field you cannot pull the string name from the class

#

unless you are inside Dog.java

pine tide
#

ohh ok ok

stark edge
#

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

pine tide
#

(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?

stark edge
#

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

#
#

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

pine tide
#

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

stark edge
#

no it is not

still trail
#

Its free

stark edge
#

make other question threads for that

pine tide
#

for what

stark edge
#

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

pine tide
#

oh ok

stark edge
#

just keeps it tidy

pine tide
#

one more question, I think I know the answer to it but not too sure

#

oh actually nvm I think I got it

stark edge
#

k

pine tide
#

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

pearl bolt