I have some question about the code below. It works correctly, but:
- Is there a better/another way to solve the exercise (as an expert would have done:) or mine it's perfectly done?
- Also, I don't see the point of using ArrayList instead of a normal vector like
int a[]
. - In the method
analysis
I wanted to set as parameters(ArrayList a,ArrayList b)
but an error (incompatible types List cannot be converted toArrayList
) appears in thisint a = Testing.analysis(seq1, seq2, t1);
line. I thought I was working withArrayList
since I instantiated my sequences as such.
Exercise:
Perform a program that uses a function that returns a 1 if two very large numbers (digits greater than 5 and where each digit is an element of the arrayList) are equal and a 0 otherwise.
Example 1:
Entry
Number 1: 2 3 4 5 6 7
Number 2: 1 4 6 8 9 0
Output "They are not equal numbers"
class Testing {
static int analysis(List a, List b, int t) throws Exception { //using
//ArrayList instead of List marks an error, why?
int c = 0;
if (a.size() == b.size() && a.size() > 5) {
for (int i = 0; i < t; i++) {
if (a.get(i) == b.get(i)) {
c++;
}
}
} else {
System.out.println("oops! the lengths are not the same or some
of the sequence is less than 6");
throw new Exception(); //to finish the method
}
if (c == t) {
return 1;
} else {
return 0;
}
}
}
public class Tarea24agoENGLISH {
public static void main(String[] args) throws Exception {
Scanner q = new Scanner(System.in);
List seq1 = new ArrayList(); //I wanted to write this List <int> seq=new ArrayList<int>(); but it marked an error, why?
System.out.println("Introduce the length of the sequence1 ");
int t1 = q.nextInt();
for (int i = 0; i < t1; i++) {
System.out.println("Introduce the digits for sequence1");
int n = q.nextInt();
seq1.add(n);
}
List seq2 = new ArrayList();
System.out.println("Introduce the length of the sequence2");
int t2 = q.nextInt();
for (int i = 0; i < t2; i++) {
System.out.println("Introdce the digits for sequence2");
int n = q.nextInt();
seq2.add(n);
}
int a = Testing.analysis(seq1, seq2, t1);
if (a == 1) {
System.out.println("same numbers");
} else {
System.out.println("\n different numbers");
}
}
}
2 Answers 2
Declaring a method
static int analysis(List a, List b, int t) throws Exception { //using
This could be just
public static int isEqual(List<Integer> a, List<Integer> b) {
This should not be an ArrayList
, as that is an implementation. You want to use the interface here because that accepts more things without adding limitations.
You have to say <Integer>
rather than <int>
because generics have to be objects, not primitives.
If you have more questions about why variants of your code are getting errors, first try searching on Stack Overflow. If you can't find anything, then post a MCVE on Stack Overflow. As was done here.
In general, you want to give methods verb names, like analyze
or isEqual
. I find isEqual
more descriptive.
You don't need to throw a checked Exception
. You could throw an unchecked exception like RunTimeException
or IllegalArgumentException
. If you throw an unchecked exception, you don't need the throws
declaration.
You don't need t
as it is just a.size()
. So just use a.size()
.
Handling exceptional cases
if (a.size() == b.size() && a.size() > 5) {
Consider flipping the logic around.
if (a.size() < 6) {
throw new IllegalArgumentException("The numbers have to have at least 6 digits.");
}
Now code and message match. We don't have to figure out that > 5
means 6 or more. And then negate that for the else
. We don't need an else
because throw
ends the execution of the method. Implicitly, the rest of the method is in an else
.
if (a.size() != b.size()) {
return 0;
}
You could throw an exception here, but as previously mentioned, this feels more like a false condition. So you can return your false value.
Early return
for (int i = 0; i < t; i++) { if (a.get(i) == b.get(i)) { c++; } }
This could be
for (int i = 0; i < a.size(); i++) {
if (a.get(i) != b.get(i)) {
return 0;
}
}
If you find an unequal value, you can return immediately.
if (c == t) { return 1; } else { return 0; }
And this could be just
return 1;
By returning early, you actually simplify the code.
Input to a list
System.out.println("Introduce the length of the sequence1 "); int t1 = q.nextInt();
This is a weird way to do it. If you do it this way, you can just use an array rather than a List
. Normally when you use a List
, you would get digits until something happened. For example, something like:
System.out.println("Introduce the digits for sequence1");
for (char digit : q.nextLine().toCharArray()) {
seq1.add(digit - '0');
}
Subtracting '0'
from digit
converts digit
from a character to a number. You could also do Character.digit(digit, 10)
which is arguably more portable and flexible.
You may have to cast to Integer
if there is a warning.
Anyway, this allows you to enter the digits all next to each other and terminate the number by hitting return.
-
\$\begingroup\$ why did you exclude the '0' character ?, in the last for loop \$\endgroup\$I likeThatMeow– I likeThatMeow2017年08月27日 03:27:53 +00:00Commented Aug 27, 2017 at 3:27
-
\$\begingroup\$ why is this
for (int digit : q.nextInt()) { seq1.add(digit);}
wrong? (I was trying to change your code to make it more descriptive) \$\endgroup\$I likeThatMeow– I likeThatMeow2017年08月27日 18:33:31 +00:00Commented Aug 27, 2017 at 18:33 -
\$\begingroup\$ That's more of a Stack Overflow question than a Code Review question. Hints: how many digits are in the number that
q.nextInt()
returns? What doesq.nextInt()
return? Thefor
loop is expecting something that implementsIterable<Integer>
there. \$\endgroup\$mdfst13– mdfst132017年08月28日 03:53:33 +00:00Commented Aug 28, 2017 at 3:53 -
\$\begingroup\$ Answer:
q.nextInt()
returns only 1 digit. So there's no way to changechar
byint
? \$\endgroup\$I likeThatMeow– I likeThatMeow2017年08月29日 02:04:39 +00:00Commented Aug 29, 2017 at 2:04 -
\$\begingroup\$ oh and I should let you know that for some reason SO block me :( so I can't post questions there anymore \$\endgroup\$I likeThatMeow– I likeThatMeow2017年08月29日 02:07:10 +00:00Commented Aug 29, 2017 at 2:07
First thing I noticed don't count how many digits are equal, soon as 1 digit is unequal return false, if the loop finishes return true. Also it seems to me if the sizes are unequal the proper response would be false not an error.
-
\$\begingroup\$ the code doesn't need to count how many digits are equal. If the loop finishes and return true then it's because the numbers are equal \$\endgroup\$I likeThatMeow– I likeThatMeow2017年08月27日 01:20:23 +00:00Commented Aug 27, 2017 at 1:20
-
1\$\begingroup\$ yes exactly as my answer states \$\endgroup\$user33306– user333062017年08月27日 01:21:23 +00:00Commented Aug 27, 2017 at 1:21
-
1\$\begingroup\$ But the exercise doesn't mention unequal lengths, it would be reasonable to assume that they would elicit the same response as any other set of numbers. \$\endgroup\$user33306– user333062017年08月27日 01:26:59 +00:00Commented Aug 27, 2017 at 1:26
-
1\$\begingroup\$ Ok, that's not mentioned in the exercise. \$\endgroup\$user33306– user333062017年08月27日 01:28:18 +00:00Commented Aug 27, 2017 at 1:28
-
1\$\begingroup\$ Since equal lengths isn't a requirement of the input your getting, returning an error is misleading and frustrating to the user. \$\endgroup\$user33306– user333062017年08月27日 01:41:31 +00:00Commented Aug 27, 2017 at 1:41
Explore related questions
See similar questions with these tags.
int[]
is not a vector, it is an array. There is no such thing as a vector in Java, except for an old, outdated class calledjava.util.Vector
that should never be used. \$\endgroup\$