Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Course Queries Syllabus Queries 2 years ago
Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
Purpose of Code
To find out what picture is on the screen by comparing it with an already existing set of pixels, which are in the form of methods that return 2D arrays, in the DataStoragePics Class.
DataStoragePics
How I tried to Solve it
methodStorage[]
tempMatrix[][]
What I need help with
When I try to solve the problem by using the steps mentioned above I get an error on the third line from the bottom in the main class repeated twice:
Multiple markers at this line - Type mismatch: cannot convert from Object to int.
I think the problem is methodStorage[x].invoke(DataStoragePicsObj) is a single array but it return a 2D array and the program doesn't recognize that so it needs either the tempMatrix to be a simple array or the methodStorage[] to be a 2D array. I need help solving that error.
methodStorage[x].invoke(DataStoragePicsObj)
tempMatrix
This is the Main Class:
Main
import java.lang.reflect.Method; int [][] tempMatrix = new int[16][450]; //Creates a DataStoragePics Object. DataStoragePics DataStoragePicsObj = new DataStoragePics(); //Stores all DataStoragePics methods in methods[]. Method[] methodStorage = DataStoragePicsObj.getClass().getMethods(); //Loops through methodStorage[]. for(int x = 0; x < method.length; x++) { //Stores a 2D array from DataStoragePics class in tempMatrix. //All methods in DataStoragePics return a 2D array with [16][10] dimensions. /*This is the error line*/ tempMatrix[16][10] = methodStorage[x].invoke(DataStoragePicsObj); /*above is the error line*/ }
This is Part of the DataStoragePics class:
public class DataStoragePics { public int[][] picXYZ() { int[][] rgbValues = { {1,2,3,4}, {9,8,7,6} }; return rgbValues; } }
I am bit of a beginner when it come to java/coding so please don't use complicated terms.
Syllabus's answer helped but I am still getting this error: "Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to [[I" The thing is casting and it stores and returns stuff on the screen. Sometimes it shows the error at the end sometimes in the middle. Don't know why.
You are looping through all the methods in your class. And casting the return value of each of these methods to int[][].
int[][]
Method[] methodStorage = DataStoragePicsObj.getClass().getMethods(); //Loops through methodStorage[]. for(int x = 0; x < method.length; x++)
This is of course going to fail, because your DataStoragePicsObj class implicitly extends java.lang.Object which has methods like hashCode, toString and getClass that do not return int[][].
DataStoragePicsObj
java.lang.Object
hashCode
toString
getClass
If you invoke a method through reflection, you should be prepared to pass it the right arguments and the process it's return value; if you cannot, you shouldn't invoke the method through reflection.
What you could do - if my understanding of what you want to do is correct - is to check the return type and also the arguments to make sure that you are prepared to handle the reflective invocation:
Method m = methodStorage[x]; if (m.getReturnType() != int[][].class || m.getParameterTypes().length != 0) { // Skip this method because it requires arguments or doesn't return int[][] continue; }
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Course Queries 4 Answers
Course Queries 5 Answers
Course Queries 1 Answers
Course Queries 3 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.