#prepping for exams, need to clear some doubts
219 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @half marlin! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 720 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
idrk if this counts as java stuff but it must be related if its in our syllabus??? I think?????
but propositional logic
might be a stupid question idk but
if the inverse of an expression is the negation of the variables, is the inverse of a'→b' = a→b or a''→b''
I know they both mean the same thing but if you had to write the inverse what would you do????
Where did you get the inverse of an expression being the inverse of the variables?
Are you looking for the contrapositive? (a -> b)' <-> b' -> a'
thats what weve got in the textbook and also what ive seen online though
both are valid inverses of the expression, they are just different ways of saying the same thing
but typically you'd just remove double negation
right okay
but if your prof wants you to write it down explicitly in a certain way, you should do that in exams
thanks
its a board exam and not in school so im going with whatevers generally acceptable
💤 Post marked as dormant
This post has been inactive for over 720 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
why isnt the object call working here???
What is ob?
object of the class Automorphic
Can you show the whole class in a codeblock?
it should be working cuz the main method calling void list does
Please format your code & make it more readable.
For the java programming language, it should look like this:
```java
public class Main {
public static void main(String[] args){
System.out.println("Hello World!");
}
```
• These are backticks, not quotes.
import java.util.*;
public class Automorphic
{
int l, u, count;
public Automorphic(int l, int u)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the lower limit of the range->");
l = in.nextInt();
System.out.println("Enter the upper limit of the range->");
u = in.nextInt();
}
boolean check(int n)
{
int sq = (n*n)/10;
if (sq==n)
return true;
else
return false;
}
void list()
{
System.out.println("Number" + "\t" + "Square");
for(int i = l; i<u; i++)
{
boolean auto = ob.check(i);
if(auto==true)
{
System.out.println(i + "\t" + i*i);
count=+1;
}
}
}
public static void main(String args[])
{
Automorphic ob = new Automorphic(0, 0);
ob.list();
}
}
oh I see
If you create an ob variable in main, it will only be valid in main
but you call ob.list (so you call list on ob) so list has access to ob using the keyword this
so you could do this.check(i)
or just check(i)
Whenever you are not in a static method, you have a "current" object (that is typically called the "receiver object")
and this just refers to that
main is static so there is no current/receiver object
there you create an Automorphic instance and call list on it so that instance is the receiver object within the list method
If you want to call another method on the current receiver object (in the same class), you can just do nameOfTheOtherMethod() or this.nameOfTheOtherMethod()
does just nameOfTheOtherMethod() work in every instance where I might otherwise use this?
yes pretty much
Hmmm, what happens, if i enter 33 for the lower and 2 for the upper bound?
And why do you need the count variable?
no numbers being checked?
You know Java Danny?
sometimes
Almost like i asked that question for Piglet to think about her code a little bit, bc in my exp that helps with learning 😁
idk, they didnt ask to check if the numbers were valid in the question so I dont have to check it
besides theres something wrong with it anyway
im not sure why but it seems like the for loop in list wont execute
also they asked for a count variable so yeah
import java.util.*;
public class Automorphic
{
int l, u, count;
public Automorphic(int l, int u)
{
l=l;
u=u;
count=0;
}
boolean check(int n)
{
int sq = n*n;
if (sq%10==n)
return true;
else
return false;
}
void list()
{
System.out.println("Number" + "\t" + "Square");
for(int i = l; i<u; i++)
{
System.out.println("y");
if(check(i))
{
System.out.println(i + "\t" + i*i);
count=+1;
}
}
System.out.println("Frequency of Automorphic numbers between " + l + "and" + u + ":" + count);
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the lower limit of the range->");
int l = in.nextInt();
System.out.print("Enter the upper limit of the range->");
int u = in.nextInt();
System.out.println();
Automorphic ob = new Automorphic(l, u);
ob.list();
}
}
the print y thing was just to see if it was ever running
I dont get it man
like, the sample inputs we're supposed to try are 1 and 1000
so int i = 1; 1<1000; i++
why does that not work??
I thought maybe it was the same issue with ob as earlier and tried it with this but that didnt change anything
okay so I entered exactly int i = 1; 1<1000; i++ to check and that worked
but the print statement after the for loop for frequency also prints them as 0 for whatever reason so there must be something to that ig
I think maybe??? making list parameterized would work????? but I dont think im allowed to do that since its specified as a non-parameterized method
you have an int l local variable
and an int l field
these are different variables
The field is for non-static methods
the local variable is valid only in the main method
but are the input values not getting assigned in the constructor
am I supposed to use this.l
ok no that didnt work either
oh I see
I didn't notice the constructor
you are doing l=l
so you are setting the parameter l to the parameter l
If you want to set the field, you need this.l
or name the variables differently
Fair 👍
But you already use i for that, no?^^
eh no i is a looping variable to check each number within the range
Ah fair
If you want to debug why the loop doesn't work, you could just print l and u at the start of the list method ^^
im gonna try doing another question now 🙏
the first frequency thingy is within the constructor, the second is the one in the list method, I added this.l but that didnt change anything
oh eait wait wait
I tried another thing
instead of l = this.l I made it this.l = l and that seems to have worked
okay so now the essence of the program is working but its not enough because I have to check more than just one digit???
Yeah, sry i didn't check the constructor.
If you have an input parameter "l" in a method and in general a field "l", then just "l" will always refer to the input parameter in the method. Therefore l=l means you asign the input parameter to the input parameter. l=this.l means you asign the value of the field to the input parameter. What you want to do is asign the value of the input parameter to the field so it gets stored in for later usage in other methods => this.l=l
Hope that helps^^
what if I count the digits in the number, like 25 = 2, so then it could be a for loop to divide until that many digits are in the thing? I feel like that would work best with a recursive method but I dont think were allowed to add more methods? I thought otherwise like, maybe a while loop to multiply 10 by how many ever digits are there in the number???
okay yay im so smart I did it
im gonna actually fail and die if it takes me this long to do one program though
itll be a written exam, I cant go back and change too much or check for errors with a compiler
Relax, if you code something for the first time it is pretty normal to take twice as long, as you later need in the exam 🙂
Even under that circumstance^^
💤 Post marked as dormant
This post has been inactive for over 720 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
what am I supposed to do about this
import java.util.*;
class Anagram
{
String s1, s2;
int l1, l2;
Anagram(String s1, String s2)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter first word->");
this.s1 = in.next().toLowerCase();
System.out.println();
System.out.print("Enter second word->");
this.s2 = in.next().toLowerCase();
System.out.println();
}
void sort(char c[])
{
char temp = ' ';
for(int i = 0; i<this.s1.length(); i++)
{
for(int j = 0; j<this.s1.length(); j++)
{
if(c[i]>c[j])
{
temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
}
boolean areAnagram(char[] str1, char[] str2)
{
str1 = sort(str1);
str2 = sort(str2);
for(int i = 0; i<s1.length(); i++)
{
if(str1[i]!=str2[i])
return false;
}
}
void check()
{
char[] str1 = new char[s1.length()];
for(int i=0; i<s1.length(); i++)
str1[i] = s1.charAt(i);
char[] str2 = new char[s2.length()];
for(int i=0; i<s2.length(); i++)
str1[i] = s1.charAt(i);
boolean a = areAnagram(str1, str2);
if(a==true)
{
System.out.println(s1 + " and " + s2 + "are anagram of each other");
}
else
System.out.println(s1 + " and " + s2 + "are not anagrams of each other");
}
public static void main(String args[])
{
Anagram ob = new Anagram("first", "second");
ob.check();
}
}
sort(char[]) sorts the given array in place and does not return anything.
Why are you using a Scanner in the constructor btw? Kinda sketchy
sketchy? 😭
I feel like its convenient
It really isn't.
Anagram(String s1, String s2) {
this.s1 = s1;
this.s2 = s2;
}
Btw, general question, why do you never add visibility modifiers to your fields and methods?
Did they not teach you private, package private, protected, public?
- the way our questions are given, we have to make our methods exactly as instructed
- I also honestly have no real idea what most of those thingies do, like, maybe some vague understanding of it but not enough to think too hard on what the limits and uses are and to use them myself
I also wonder, why you don't do the anagram check in the constructor, does the exercise directly discourage that?
wait im just gonna show you the question
- I doubt that they tell you to write a constructor with two input parameters just to never use them but a scanner instead.
- Your teachers seem to bad at their job.
to 2, my last years teacher taught me enough to be able to code well enough for what we have to do (which is pretty great since she got me getting grades above that of my classmates who had studied it for 2 years prior while I was learning it for the first time) but my teacher this year...yeah she sucks
There is nothing said about Scanners, but yeah, the check shouldn't be called in the constructor, my bad.
They really don't tought you visibility... man this sounds like a lousy course
oh no its not the course thats bad
we do have it in our syllabus
I just need to catch up on that part
thing is since this is how all the programming questions are asked, I didnt need to know it cuz those specifiers at most come for like 1 mark questions
The materials kinda imply a bad "teaching order". Or to be more direct: I think they should use C or Python to teach you the kind of stuff you asked so far^^
Ahhhh, so they teach you the object oriented stuff later? Still a bit weird, but that would make sense
nonono all of the stuff is there we have a couple of chapters on that
its just not given much weightage in the exam paper
like, theyd just ask maybe a 2m question like "what is the purpose of the keyword private" maybe
perhaps, I wouldnt know tbh
Yeah cool, but why do they not tell you to use those keywords, since no visibility is bad coding style xD
Jup, i don't blame you. I just get annoyed at bad teachers.
so valid ive spent all year complaining to any and all about these people
is it? 👀
Yes...
do they make videos/guides on what good coding style is? like I know from the whole yandere simulator debacle that spaghetti code exists but besides just:
- weird logic
- low scope for adding further content or easily making changes
- being barely understandable to other coders
idrk whats "bad"
Heh...
- What do you mean by weird logic?
- It surely is easier if code doesn't contain, 30000 nested scope, but that is not a "hard" coding convention.
- That is why documentation exists.
I forgor that scope was a java thing in and of itself
dont know what it is but its there
Scope is a thing for every imperative programming language i know😅
Scope is basically the space between { and }
I think weird logic is like...excessively convoluted ways of achieving the programs desired output? like for example, you need to extract each digit from a number and youre converting it into a string and using a method something (String x, int y) to assign it to entirely seperate char variables (as opposed to using an array) and then converting it into integers to do whatever else you need to
sure it could work and im all for "if it works, it works" but afik coding logic is supposed to be simple and efficient above all
Btw, did you know the String method toCharArray()
ah okay
Yeah, that is all fair, but isn't what i meant by "coding conventions". Dw about that now though, since the exam doesn't seem to give a shit either xD
then what do you mean?
Optimization comes after the initial development. First get your stuff running, then think about making it better.
For example:
- Formatting guidelines
- Using the correct keywords explicitly
- Catching exceptions
Stuff like that^^
ahh okay
There was a typo in your check btw
Btw, why are you using s1.length() in sort, when you wanna sort the c
And your areAnagram method is vunerable, bcs it doesn't check, whether the input parameters are of equal length
Man this exercise is also so flawed from an object oriented design perspective, but whatever...
theyre both the same size tho
I know, I was wondering if I should check that
dont know what you mean but okay lol
Lemme write you a main that disproves that^^
Ye, check it
public static void main(String[] args) {
Anagram a = new Anagram("hello", "elloh");
a.sort("cba".toCharArray());
}
Execute that and see what happens
🤷♂️ 😂
^
yeah that didnt work 😭
wait a second
I dont need to assign values
that works
It would have if you changed the void in void sort.
okay now theres a logic error -_- ill try to fix this
oh shit I forgot to use the instance variables in the question
No, sets and test are indeed not anagrams
OH
right
okay but it doesnt work still
there can only be an issue where im comparing the words after sorting
skibidi
okay ah. some weird shit is happening to the second array
wait can u show the code
wow I have never seen that thing before
ah okay wait
i think the sorting part doesnt work
still doesnt change that null error
Yeah, bcs you still have the typo in check
This seems over complicated, but as long as it works 🤷♂️
its so sad this program had to take so long 😔
the one after this took me only like 15mins or something
and it was actually fully functional
Happens :/
💤 Post marked as dormant
This post has been inactive for over 720 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
either make sort return the sorted array or just do sort(str1) without the str1 =
yeah but then the next part doesnt work
hm?
thinking about this made me realize that there would probably be a reverse function, turning a character array into a string and I looked it up and apparently you can do this: String str1 = new String(ch_arr);, which I think would work out
idk that was how the code was before and it didnt work so I changed it to that
wait no
hgh whatever
@swift gate imma try the code you sent me now
💤 Post marked as dormant
This post has been inactive for over 720 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
okay so apparently acc to a quick google search you can use multiple constructors but why??? I thought that maybe it was a mistake but its in the answer key too so im not sure
In the end constructors are also just functions with an implicit return value.
ugh okay theyre doing something here I dont know yet and im gonna just ask my friend to teach it to me later
If you declare multiple constructors, you can choose one of them when creating an object of the class
You can also do that with every other method. There can be like a bazillion "print" methods for example, as long as they have different input parameters
So called Polymorphism
OHHHHH we have that thing in our syllabus
thats good to know cuz I dont know it besides having learnt the basic concept of it and earlier I was slightly worried my friend might not be familiar with the thing either
The thing is Polymorphism is more than that
Interfaces and inheritance can be interpreted a polymorphic concept aswell
yeah ik the general idea and vaguely remember the syntax and all but not enough to use it
Good luck for your exams!
Anything I could answer too, I was looking to test myself by trying to explain stuff if I knew it
💤 Post marked as dormant
This post has been inactive for over 720 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
if theres an instance variable whose value im changing in a recursive void method when I use it in another void method will I get the original or changed value
If both are called on the same instance, they share the fields/instance variables
If a non-static method calls another non-static method in the same class without explicitly specifying a receiver object/instance (so when you are doing someMethod() instead of someObject.someMethod()), that is the case
right okay thanks