#Very simple Java questions

1 messages · Page 1 of 1 (latest)

zinc nova
#

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

gusty flower
#

can you post what you have so far so that we can help you?

zinc nova
#

wdym

#

this is what i have so far

#

@gusty flower

gusty flower
#

are you able to use the substring method?

zinc nova
#

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

gusty flower
#

tell me in english what you think you need to do and maybe it will help you actually write the code

zinc nova
#

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

gusty flower
#

do you know what a class is in java?

zinc nova
#

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

gusty flower
#

it's np

zinc nova
#

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

gusty flower
#

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

zinc nova
#

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

gusty flower
#

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

zinc nova
#

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

gusty flower
#

its stated it's event

zinc nova
#

id divide it by 2 but first id need to get the length

gusty flower
#

even

zinc nova
#

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

gusty flower
#

what will that give you

#

what does that represent to you?

zinc nova
#

wdym

gusty flower
#

or what would you call the variable?

zinc nova
#

sum like

gusty flower
#

sum?

zinc nova
#

no like

gusty flower
#

like the addition of 2 numbers?

zinc nova
#

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

gusty flower
#

name.substring(divided number int variable, number length)

zinc nova
#

secondHalfStr

#

idk

gusty flower
#

ok thats fine

zinc nova
#

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

gusty flower
#

what would be the second argument to the substring function?

zinc nova
#

i hope u understand my lucridous thoughts btw

#

wait wdym by second arghument

gusty flower
#

name.substring(divided number int variable, number length)

#

there is 2 arguments here

#

there is a , in between them

zinc nova
#

it would be 0? like the start of the str

gusty flower
#

thats one argument

#

whats the other one?

#

maybe just write out what the name.substring() would look like

zinc nova
#

name.substring(numberLength, 0)

gusty flower
#

the start of the substring would be the length index and the end would be 0?

zinc nova
#

ohh i see what ur getting to

#

other way around right

#

so 0, numberLength

gusty flower
#

numberLength is name.Length()?

zinc nova
#

no numberLength would be divided number int variable thing

gusty flower
#

oh ok

zinc nova
#

so if the entire string length is 10

#

it would be 5

#

0,5

#

and 5 would be stored in it

gusty flower
#

oh ok

zinc nova
#

how would i begin to write this all out?

gusty flower
#

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

zinc nova
#

do ineed any of the like

#

public class

#

private whatever

#

no because im not writing a whoel class right

gusty flower
#

no for class for for the method maybe

zinc nova
#

wdym for the method?

gusty flower
#

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

zinc nova
#

would it be somethign like this

#

and then i add the like

#

public class Main

gusty flower
#

you don't need the class part

#

the question says you don't

zinc nova
#

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

gusty flower
#

well you need to concatenate the final strings parts

#

and is str a data type?

zinc nova
#

like

#

str = string

#

kinda like int stores an integer value or wh atevr

#

right

#

isnt that how that works

gusty flower
#

for int yeah

zinc nova
#

and how would i concatenate the final part

gusty flower
#

for string I don't think so

zinc nova
#

wait

#

is string just string

gusty flower
#

very close

#

but you need to make one change for it to work

zinc nova
#

String

gusty flower
#

there you go

zinc nova
#

ok awesome

#

then the concatenation thing

gusty flower
#

now how do you combine two strings

zinc nova
#

"string1" + "string2"?

gusty flower
#

yeah

zinc nova
#

but its a string stored in a variable

#

so i wouldnt use "

#

id just put like

#

secondHalfStr + firstHalfStr

#

?

gusty flower
#

there you go

zinc nova
#

shoul di keep the string str = nm,ae thing

#

i put that randomly

gusty flower
#

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

zinc nova
#

()

gusty flower
#

yeah

zinc nova
gusty flower
#

oh

#

it's still wrong though

#

it's not capital

zinc nova
#

capit

#

eya

#

ok

#

done

gusty flower
#

ok lets see it

zinc nova
#

do i keep the string str = name thing

gusty flower
#

yeah you would need the name

zinc nova
#

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

gusty flower
#

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

zinc nova
#

would i do like

#

a user input thing

gusty flower
#

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) {

}

zinc nova
#

in a completely different part of the assingment he did this

#

do i need to do any of that

#

brb 5 mins btw

gusty flower
#

yeah you can just copy what they have and place your code inside

#

like
public void methodName(parameters){
code goes in here
}

zinc nova
#

what parameters should i put

#

im back

#

@gusty flower \

gusty flower
#

you just need the initial string

#

that you want to modifiy

zinc nova
#

so

gusty flower
#

the string is written in lower case and you are missing the parameter name

zinc nova
#

wdym parameter name

#

i named it awesomeMethod

#

instead of methodName

gusty flower
#

thats the method name

#

the parts that are inside of the ()

#

you have a type but no name

zinc nova
gusty flower
#

look at this

#

there is 2 parts

#

you had the type

#

but not a name

zinc nova
#

oh

#

olike

#

ohh

#

like

#

(string methodName)

#

String methodName

#

capitalized

gusty flower
#

like public String awesomeMethod(String name)

zinc nova
#

is String in String name defining that name will be a string value

gusty flower
#

it's the name of the string that you will use to change it

zinc nova
#

ok figured

#

so im good like this then right

gusty flower
#

and you want to return final string at the end

#

just return finalStr

#

and put { around the code}

zinc nova
#

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

gusty flower
#

you would need the static as well

#

I forgot about that

zinc nova
gusty flower
#

the void needs to be string

#

void means you won't return anything

zinc nova
#

wdym

#

so where do i put it

gusty flower
#

public static String awesomeMethod(String name)

zinc nova
#

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

gusty flower
#

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

zinc nova
#

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?

gusty flower
#

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

zinc nova
#

nah he was being like

#

a dickhead

gusty flower
#

oh

zinc nova
#

like

gusty flower
#

oh but static is like this

#

like how you have string.length()

zinc nova
#

yea

gusty flower
#

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

zinc nova
#

oh okay

#

i kinda understand that

gusty flower
#

you ever use the math class?

#

like Math.pow()?

zinc nova
#

i dont think so

#

do u know what this means

#

i might but idk

gusty flower
#

you know what the methods are?

#

like String.substring()?

zinc nova
#

1 sec

#

i might

gusty flower
#
#

these are the math methods

#

based on the question you can use 3 of these to simplify your answer

zinc nova
#

would i set it up like this

gusty flower
#

status should be static

#

and this problem is asking for 4 parameters

#

not just the 1 this time

zinc nova
#

i meant to put that my fault

#

so like

#

int x1,x2,y1,y2

gusty flower
#

I think you need the type after each parameter

#

like int x1,int x2,int y1,int y2

zinc nova
#

does pemdas work in this situation like

#

(int(x1,x2,y1,y1))

gusty flower
#

I don't think that is related to pemdas

zinc nova
#

nevermind

#

so

gusty flower
#

but I think you need to write it out how I wrote it afaik

zinc nova
gusty flower
#

yeah the x1 stuff is good

#

but it's still static

zinc nova
#

omg my fault

gusty flower
#

also do you know what the difference is between and int and a double is?

zinc nova
#

yes

#

decimal

#

2.0 2

#

should it be a double

gusty flower
#

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

zinc nova
#

okay

#

done

#

i changed it all

#

to double

gusty flower
#

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?

zinc nova
#

i know math dw i just dont know what methods id use

gusty flower
#

ok did u go to that site?

zinc nova
#

yea looking thru it rn

#

so ihave to use 3 of these

gusty flower
#

yeah it's not random though

zinc nova
#

ik

gusty flower
#

the math problem is looking for 3 specific things you have to do

zinc nova
#

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

gusty flower
#

sqrt is one

#

squared is called power

#

so Pow

#

square is power raised to 2

#

and there is one more thing you need

zinc nova
#

are these math methods or ap subset methods btw

#

bc i have specific ap subset methods i think

#

i found it

zinc nova
#

omg

#

these are the ones i use

gusty flower
#

math was listed here

zinc nova
#

yea

#

oh

#

idk

#

the thing i just sent u is what i can use

gusty flower
#

but I dunno maybe check your listing

#

in your book

zinc nova
#

so

gusty flower
#

I see math in your listing

zinc nova
#

yes i know

#

i just dont know how to set them up to work if that makes sense

gusty flower
#

break the problem down into parts

#

so maybe work from the inside out

zinc nova
#

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

gusty flower
#

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

zinc nova
#

ohhhhhhhhhhhhhh

#

so

#

it would be something like

gusty flower
#

like java double xSum=pow(abs(x1-x2))

zinc nova
#

yeah

#

i was gonna say

gusty flower
#

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

zinc nova
#

double xSumPre = abs(x1-x2)
double xSumPos = pow(xSumPre)

gusty flower
#

yeah that would work

zinc nova
#

then

#

id do the same for y

#

right

#

then id do

#

double totalSum = sqrt(xSumPos + ySumPos)

gusty flower
#

yeah

zinc nova
#

return totalSum

gusty flower
#

yeah

#

I think you pretty much got it this time

zinc nova
#

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

gusty flower
#

it's ok

zinc nova
#

im a horrible procrastinator sadly

gusty flower
#

but the input in the question is ints

zinc nova
#

why

#

the values of x and y dont have to be ints

gusty flower
#

I dunno thats what they said

zinc nova
#

oh

#

my fault

#

it does say that

#

that means there would be no doubles then no?

#

wait nvm

#

sqrt

#

iforgot

gusty flower
#

yeah you got it

zinc nova
#

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

gusty flower
#

when using the math methods you need to do Math.pow() or Math.sqrt() I don't think they work alone

zinc nova
#

oh where

#

would i put that

gusty flower
#

replace pow with Math.pow

#

and same thing with abs

zinc nova
#

oh i get what u mean

gusty flower
#

and I think doubles should all be lowercase

zinc nova
#

yea

#

i typoed

#

it auto uppercases it

#

capitalizes

#

sorry im high

#

yo i appreciate u

#

like actually tysm

gusty flower
#

np

#

besides that your just missing the { and ; after every line and the } at the end

zinc nova
#

ik

#

he didnt put it in the example so i figure he doesnt want it

gusty flower
#

oh ok

#

and there is a random \ on one of the lines

#

prob a typo though

zinc nova
#

oh yea

#

man why tf is java so much more complicated and obese compared to javascript

gusty flower
#

you will get used to it

zinc nova
#

yea sadly

gusty flower
#

it's got it's up and downs

zinc nova
#

i cant wait to transform into afemboy after gaining fluency in java

gusty flower
#

oh well gl with that

zinc nova
#

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

gusty flower
#

it's what you use to take in user input I think

zinc nova
#

yea

#

sum like that

#

ill ttyl bro i appreciate it its late and i need to pass out and go to school tmr

#

BYe ❤️

gusty flower
#

later