#prepping for exams, need to clear some doubts

219 messages · Page 1 of 1 (latest)

half marlin
#

I have my computer exam in a month, and as I study I might need to clear up some doubts.

-# Yes, I have a teacher, but she genuinely sucks.

light lionBOT
#

This post has been reserved for your question.

Hey @half marlin! Please use /close or the Close Post button 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.

half marlin
#

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

crisp idol
#

Are you looking for the contrapositive? (a -> b)' <-> b' -> a'

half marlin
crisp idol
#

ok ok

#

yeah I don't know the notation of calling that "inverse statement"

crisp idol
#

but typically you'd just remove double negation

half marlin
#

right okay

crisp idol
#

but if your prof wants you to write it down explicitly in a certain way, you should do that in exams

half marlin
#

thanks

half marlin
light lionBOT
#

💤 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.

half marlin
#

why isnt the object call working here???

crisp idol
#

What is ob?

half marlin
#

object of the class Automorphic

crisp idol
#

Can you show the whole class in a codeblock?

half marlin
#

it should be working cuz the main method calling void list does

light lionBOT
#

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.

half marlin
#
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();
    }
}
crisp idol
#

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)

half marlin
#

this is so weird man

#

mostly because I didnt learn it but still

crisp idol
#

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

half marlin
#

does just nameOfTheOtherMethod() work in every instance where I might otherwise use this?

crisp idol
#

yes pretty much

swift gate
swift gate
crisp idol
swift gate
#

Almost like i asked that question for Piglet to think about her code a little bit, bc in my exp that helps with learning 😁

half marlin
#

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

crisp idol
#

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

half marlin
#

but are the input values not getting assigned in the constructor

#

am I supposed to use this.l

#

ok no that didnt work either

crisp idol
#

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

swift gate
half marlin
swift gate
half marlin
#

im gonna try doing another question now 🙏

half marlin
#

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

swift gate
# half marlin instead of `l = this.l` I made it `this.l = l` and that seems to have worked

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^^

half marlin
#

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

swift gate
light lionBOT
#

💤 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.

half marlin
#

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();
    }
}
short solstice
#

sort(char[]) sorts the given array in place and does not return anything.

silent needle
#

yes

#

so the array you provided is sorted

swift gate
half marlin
#

I feel like its convenient

swift gate
#
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?

half marlin
#
  1. the way our questions are given, we have to make our methods exactly as instructed
  2. 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
swift gate
#

I also wonder, why you don't do the anagram check in the constructor, does the exercise directly discourage that?

half marlin
#

wait im just gonna show you the question

swift gate
half marlin
#

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

swift gate
half marlin
#

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

swift gate
swift gate
half marlin
#

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

half marlin
half marlin
swift gate
swift gate
half marlin
#

so valid ive spent all year complaining to any and all about these people

swift gate
half marlin
#

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"
swift gate
half marlin
#

I forgor that scope was a java thing in and of itself

#

dont know what it is but its there

swift gate
swift gate
half marlin
# swift gate Heh... * What do you mean by weird logic? * It surely is easier if code doesn't ...

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

swift gate
half marlin
#

no way

#

thats so cool

half marlin
swift gate
half marlin
#

then what do you mean?

swift gate
swift gate
half marlin
#

ahh okay

swift gate
#

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

swift gate
half marlin
half marlin
half marlin
swift gate
swift gate
swift gate
#

Execute that and see what happens

half marlin
#

I will but wait

#

I never solved the issue in the first place

swift gate
#

🤷‍♂️ 😂

half marlin
#

yeah that didnt work 😭

#

wait a second

#

I dont need to assign values

#

that works

swift gate
swift gate
#

That is the other option

half marlin
#

okay now theres a logic error -_- ill try to fix this

#

oh shit I forgot to use the instance variables in the question

swift gate
half marlin
#

OH

#

right

#

okay but it doesnt work still

#

there can only be an issue where im comparing the words after sorting

silent needle
#

skibidi

half marlin
#

okay ah. some weird shit is happening to the second array

silent needle
#

wait can u show the code

half marlin
#

wow I have never seen that thing before

silent needle
#

oof cant open on mobike

#

mobile

half marlin
#

ah okay wait

silent needle
#

i think the sorting part doesnt work

swift gate
#

Yes the sorting is cooked

#

j should be i + 1 instead of 0

#

Initially

half marlin
#

still doesnt change that null error

swift gate
half marlin
#

I fixed that

#

I need to make assigning it to each string possible somehow

swift gate
half marlin
#

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

swift gate
#

Happens :/

light lionBOT
#

💤 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.

crisp idol
half marlin
crisp idol
#

hm?

half marlin
half marlin
# crisp idol hm?

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

light lionBOT
#

💤 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.

half marlin
#

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

short solstice
#

In the end constructors are also just functions with an implicit return value.

half marlin
#

ugh okay theyre doing something here I dont know yet and im gonna just ask my friend to teach it to me later

crisp idol
#

If you declare multiple constructors, you can choose one of them when creating an object of the class

swift gate
#

So called Polymorphism

half marlin
#

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

swift gate
#

Interfaces and inheritance can be interpreted a polymorphic concept aswell

half marlin
#

yeah ik the general idea and vaguely remember the syntax and all but not enough to use it

vale coral
#

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

light lionBOT
#

💤 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.

half marlin
crisp idol
crisp idol
half marlin
#

right okay thanks