#Very simple Java questions
1 messages · Page 1 of 1 (latest)
so
im bascially in my first week of using java and i need help with thesse questions
you can see i started out trying to do the part a
but idk where to go from there
can you post what you have so far so that we can help you?
are you able to use the substring method?
i have like
no idea what im doing
i have these
teacher gave them to me
i kinda see what i need to do but i dont know how
tell me in english what you think you need to do and maybe it will help you actually write the code
before i do that
my treacher said that in these 2 questions im not writing a whole class just code segments
what does that mean for me
do you know what a class is in java?
sorry if i like sound like a dickhea dbtw im like doing a bunch of shit ayt once for like 6 classes and going crazy mentally
kinda
it's np
like
isnt there
a class and then like
u use the class
the class is how u make like objects
and objects let u do shit and store values and methods and stuff
so you don't need to make all of that just a working function
in java you need to write out a lot to get stuff done but in other languages you can get more to the point if you want to call it that
and for the question you can just get right to the point
okay
this is gonna sound stupid but why do i get to get right to the point
just bc my teacher is letting me?
like wouldnt i need to have a class that defines methods to use in this function thing
for you to write it on the computer you would need to have a class
but I guess since it's just a question on a paper it's not necessary
alright
so
back to the whole
explainuing what id have to do in english thing
i believe id have to
first check if the string is an even number
then divide it by 2
hold on
its stated it's event
id divide it by 2 but first id need to get the length
even
okay
then id just get its length
name.Length()
then like
divide it and store the divided variable in an int variable
and then use the like
name.substring(divided number int variable, number length)
and store it in a variable
wdym
or what would you call the variable?
sum like
sum?
no like
like the addition of 2 numbers?
something like uhh
no
sum as in something
abbreviated lol
i guess like
well which variable
the one that would be storing the length
the one that stores the divided length
or the final substring thing
name.substring(divided number int variable, number length)
ok thats fine
and lastly id save the str value as like
secondHalfStr and then the firstHalfStr which would be defined as the same method but instead of the substring being the number length it wo uld be 0
b/c first index thing
to the half
what would be the second argument to the substring function?
name.substring(divided number int variable, number length)
there is 2 arguments here
there is a , in between them
it would be 0? like the start of the str
thats one argument
whats the other one?
maybe just write out what the name.substring() would look like
name.substring(numberLength, 0)
the start of the substring would be the length index and the end would be 0?
numberLength is name.Length()?
no numberLength would be divided number int variable thing
oh ok
so if the entire string length is 10
it would be 5
0,5
and 5 would be stored in it
oh ok
how would i begin to write this all out?
well lets write down what we got
first the number length
then write down the first half
then write down the second half
and then we should have the parts that we need to answer the question
do ineed any of the like
public class
private whatever
no because im not writing a whoel class right
no for class for for the method maybe
wdym for the method?
the function
but maybe you don't even need that
I'm not sure if you need it or not
public class Main {
static void myMethod() {
// code to be executed
}
}
it's normally setup like this
the modifiers change depending on what you are trying to make
and you might need the static void methodName {}
I'm not sure if u do or not
the code generally goes inside of that
it doesn't hurt to add it
ok so i shouldnt
what should i put here
and like
idk i just feel like what i wrote isnt fully correct
isnt it missing something
like
str = string
kinda like int stores an integer value or wh atevr
right
isnt that how that works
for int yeah
and how would i concatenate the final part
for string I don't think so
String
there you go
now how do you combine two strings
"string1" + "string2"?
yeah
but its a string stored in a variable
so i wouldnt use "
id just put like
secondHalfStr + firstHalfStr
?
there you go
there is a problem with how you wrote the definition of lengthStr
you had it correct earlier
but now it's not written correctly
something wrong with the right side of the equals
()
yeah
ok lets see it
yeah you would need the name
do i bneed the str part tho
like how does it even work
the string str = name pa rt
i just wrote it down nota ctually thinking abt how it does anything
well technically not
cause they would give it to you
but if ur writing it for a question you might as well make that clear
oh
wait no
it would just cause an error
cause it's like saying make a string named str and set it equal to name
actually it might be ok
you can just put it as an argument for a method though
cause the method would need some kind of input to do the operations on
static void myMethod(String name) {
}
in a completely different part of the assingment he did this
do i need to do any of that
brb 5 mins btw
yeah you can just copy what they have and place your code inside
like
public void methodName(parameters){
code goes in here
}
the string is written in lower case and you are missing the parameter name
thats the method name
the parts that are inside of the ()
you have a type but no name
like public String awesomeMethod(String name)
is String in String name defining that name will be a string value
it's the name of the string that you will use to change it
and you want to return final string at the end
just return finalStr
and put { around the code}
okay cool
so in actual java
to use this
theyd call awesomeMethof
d
in their code right
like
with the parameter being whatever they want to switch around
so like
awesomeMethod(Ball) would return alba
i mean
llba
public static String awesomeMethod(String name)
can u explain like
what that does
does that make it so it knows that a string type will be returned
so like if i wanted to return an int value would i put public static int
yeah
public determines who can use it
static is about classes
like if you want it to be related to the entire class or just an instance of the class
ok
tysm so far btw
i joined a diff server and this like
the least helpful person in the universe was "helping" me
can we move on to the part b now then?
oh everyone is a beginner so maybe they know what to do but are not really good at explaining or
or they could just not be familiar with java also
could be a lot of things
oh
like
yea
it gives you the length of a string that you are using
so you run it with the actual object
but if you wanted to take a string as a parameter
you could have a static Length(String str) function
that would give you the length of any string you put in
these are the math methods
based on the question you can use 3 of these to simplify your answer
would i set it up like this
status should be static
and this problem is asking for 4 parameters
not just the 1 this time
I don't think that is related to pemdas
but I think you need to write it out how I wrote it afaik
omg my fault
also do you know what the difference is between and int and a double is?
ok so the equation that you were given looks like it might result in a number that isn't a whole number
so you might want to return a double
ok
now you just need to use the math methods
to with the points they gave you
do you have a good understanding of what the question is like what that formula is representing and what x1 and x2 and y1 and y2 is?
i know math dw i just dont know what methods id use
ok did u go to that site?
yeah it's not random though
ik
the math problem is looking for 3 specific things you have to do
so
first id find x1-x2 squared and then for y
and then the sqrt of them added together
so like
i could do xTotal
yTotal
sumTotal
sqrt is one
squared is called power
so Pow
square is power raised to 2
and there is one more thing you need
are these math methods or ap subset methods btw
bc i have specific ap subset methods i think
i found it
math was listed here
so
3 parts right
3 methods
so
one would be sqrt(totalSum) totalSum being equal to xSum + ySum
then
would i do double xSum = abs(x1 -x2) how would i use pow in this
that is fine to get the abs val
after you have that number
you can run pow on it
like xSum=pow(xSum)
or you can put it inside of the pow function
like java double xSum=pow(abs(x1-x2))
you generally have to use Math.pow or Math.abs though
I don't think it would work on it's own
but I don't remember
double xSumPre = abs(x1-x2)
double xSumPos = pow(xSumPre)
yeah that would work
then
id do the same for y
right
then id do
double totalSum = sqrt(xSumPos + ySumPos)
yeah
return totalSum
the reason why i have so much trouble rn is because i have 0 foundational java skills
im learning everything before this as i go
it's ok
im a horrible procrastinator sadly
but the input in the question is ints
I dunno thats what they said
oh
my fault
it does say that
that means there would be no doubles then no?
wait nvm
sqrt
iforgot
yeah you got it
I freaking hate how much i procrastinate
i literally skipped summer school and missed algebra 2 and precalc and went straight from geo to ap calc bc and somehow passed iwth a 4 last year
Like it was so stressful
when using the math methods you need to do Math.pow() or Math.sqrt() I don't think they work alone
oh i get what u mean
and I think doubles should all be lowercase
yea
i typoed
it auto uppercases it
capitalizes
sorry im high
yo i appreciate u
like actually tysm
np
besides that your just missing the { and ; after every line and the } at the end
you will get used to it
yea sadly
it's got it's up and downs
i cant wait to transform into afemboy after gaining fluency in java
oh well gl with that
thank you
ill probably be back here with more questions
like i donte ven know what a scanner is
LIke i have no idea
Zilch zero
it's what you use to take in user input I think
yea
sum like that
ill ttyl bro i appreciate it its late and i need to pass out and go to school tmr
BYe ❤️
later