I am trying to access and store information in the array 'qmissed' from the method 'questionsMissed', how would i do that?
Thank you for your help!
public class DriverExam {
public void makeMissedArray(){
int smissedarray = totalIncorrect();
int[] qmissed = new int[smissedarray];
}
public int[] questionsMissed(){
if(totalIncorrect() > 0){
makeMissedArray();
}
int x = 0;
if(totalIncorrect() == 0){
return qmissed;
}
for(int i = 0; i < 20; i++){
if(correct[i] != student[i]){
qmissed[x] = (i+1);
x++;
}
}
return qmissed;
}
}
Looks like you want qmissed to be an instance variable:
public class DriverExam {
int[] qmissed;
public void makeMissedArray(){
int smissedarray = totalIncorrect();
qmissed = new int[smissedarray];
}
...
}
There will be one copy of qmissed for each instance of the DriverExam class, and it can be accessed by any instance method of the class.
Related
Trying to get the return function to return the full array, but not able to do it with a for loop or without. Is there any way to get this done?
public class Teams {
String Game;
String Coach;
String Player[];
public void setPlayer(String a[])
{
for (int i = 2; i<10; i++)
{
Player[i-2] = a[i];
}
}
public String getPlayer()
{
for(int i = 0; i < 8; i++)
{
return Player[i];
}
}
}
There's nothing special about arrays - you can return the member as is:
public String[] getPlayer() {
return Player;
}
Below is my code and I have notes beside where my errors are showing. Im unsure where I am going wrong when recalling my method or if that is even the issue.
import java.util.Scanner;
public class HurlerUse
{
static Hurler[] hurlerArray;
// find lowest score (static method)
public static int findLow(Hurler[] hurlerArray)
{
for(int i = 0; i < hurlerArray.length; i++)
{
int lowest = 0;
int index = 0;
for(int j=0; j<hurlerArray.length; j++)
{
int current = hurlerArray[i].totalPoints();// issue with my method 'totalPoints'
if(current < lowest)
{
lowest = current;
index = i;
}
}
return index;
}
}
//main code
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Hurler[] hurlerArray = new Hurler[5];
for (int i = 0; i <4; i++)
{
hurlerArray[i] = new Hurler();
System.out.println ("Enter Hurler Name:");
hurlerArray[i].setName(sc.nextLine());
hurlerArray[i].setGoalsScored(sc.nextInt());
System.out.println("Enter the hurler's goals scored");
hurlerArray[i].setPointsScored(sc.nextInt());
System.out.println("Enter the hurler's points scored");
}
for(int i=0;i< hurlerArray.length; i++)
{
hurlerArray[i] = new Hurler(MyName, MyGoalsScored, MyPointsScored);// issue with all 3 objects in the brackets but im unsure of how to fix them
}
System.out.println("The lowest scoring hurler was " + hurlerArray[findLow(hurlerArray)].getName());// error with my code here I think it is in the method
}
}//end of class
I know the nyName, myGoalsScored, myPointsScored is incorrect but can anyone explain why?
This is the class page that accompanies it
public class Hurler
{
private String name;
private int goalsScored;
private int pointsScored;
public Hurler() //constructor default
{
name ="";
goalsScored = 0;
pointsScored = 0;
}
public Hurler(String myName, int myGoalsScored, int myPointsScored) // specific constructor
{
name = myName;
goalsScored = myGoalsScored;
pointsScored = myPointsScored;
}
//get and set name
public String getMyName()
{
return name;
}
public void setName(String myName)
{
name = myName;
}
//get and set goals scored
public int getGoalsScored()
{
return goalsScored;
}
public void setGoalsScored(int myGoalsScored)
{
goalsScored = myGoalsScored;
}
// get and set points scored
public int getPointsScored()
{
return pointsScored;
}
public void setPointsScored(int myPointsScored)
{
pointsScored = myPointsScored;
}
public int totalPoints(int myGoalsScored, int myPointsScored)
{
int oneGoal = 3;
int onePoint = 1;
int totalPoints = ((goalsScored * oneGoal) + (pointsScored * onePoint));
{
return totalPoints;
}
}
}//end of class
You call totalPoints() without parameters while method totalPoints(int, int) in Hurler class expects two int parameters.
Objects MyName, MyGoalsScored, MyPointsScored are not declared at all.
You call getName() method, while in Hurler class you do not have one. There is method getMyName(), maybe you want to call that one.
I excepted that my functions in Level.java class allow me to make 2D array copy at any time in my program then change size of level array and fill it with values of copy and at last display it.
When I try to run my program it shows NullPointerException at line 24 in Level.java (a part which replaces the values).
Game.java Class
package main;
public class Game {
public static void main(String[] args) {
Level lvl = new Level();
char[][] exampleLevelTemplate = new char[][] {
{'#','#','#'},
{'#','#','#'},
{'#','#','#'}
};
lvl.setLevelSize(3,3);
lvl.setLevelLayout(exampleLevelTemplate);
lvl.displayLevel();
}
}
Level.java Class
package main;
public class Level {
private int levelWidth;
private int levelHight;
private char[][]level;
public void setLevelSize(int Width,int Height)
{
levelWidth = Width;
levelHight = Height;
char[][]level = new char[levelWidth][levelHight];
}
public void setLevelLayout(char[][]levelTemplate)
{
int a;
int b;
for(a=0; a < levelWidth; a++)
{
for(b=0; b<levelHight; b++)
{
level[a][b] = levelTemplate[a][b]; //Error happens here
}
}
}
public void displayLevel()
{
int a;
int b;
for(a=0; a < levelWidth; a++)
{
for(b=0; b<levelHight; b++)
{
System.out.println(level[a][b]);
}
}
}
}
Change your setLevelSize method to this:
public void setLevelSize(int Width,int Height)
{
levelWidth = Width;
levelHight = Height;
level = new char[levelWidth][levelHight];
}
You will see that line:
char[][]level = new char[levelWidth][levelHight];
was changed to:
level = new char[levelWidth][levelHight];
You need to just assign the array Object to array reference variable "level" and not create a local one and initialize it like you did.
You have been assigning value to a null array reference variable and therefore got NullPointerException.
I have two ArrayLists. How can I compare the elements in the arraylists and create a new list with the results?
I need to iterate through the list to actually get its results and compare. How can I do it in Java?
Any help is appreciated.
Thanks
You can compare by implementing Comparator class.You can understand by below example in which Two Time class is getting compared.
class Time
{
int hours,minutes,seconds;
public Time(int hours,int minutes,int seconds)
{
this.hours=hours;
this.minutes=minutes;
this.seconds=seconds;
}
public int getHours()
{
return this.hours;
}
public int getMinutes()
{
return this.minutes;
}
public int getSeconds()
{
return this.seconds;
}
public String toString()
{
return String.format("%d:%02d:%02d %s",((hours==0||hours==12)? 12:hours%12),this.minutes,this.seconds,((hours>=12)?"PM":"AM"));
}
}
class TimeComparator implements Comparator<Time>
{
public int compare(Time t1,Time t2)
{
int hours=t1.getHours()-t2.getHours();
int minutes=t1.getMinutes()-t2.getMinutes();
int seconds=t1.getSeconds()-t2.getSeconds();
if(hours>0)
return 1;
else if(hours<0)
return -1;
if(minutes>0)
return 1;
else if(minutes<0)
return -1;
if(seconds>0)
return 1;
else if(seconds<0)
return -1;
return 0;
}
public boolean equals(Object obj)
{
return true;
}
}
After this you can use Collections class to use any method like sorting or search.
I hope that the following chunck of code will get you started.
import java.util.ArrayList;
class Compare {
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
a.add("Hello");
a.add("Goodbye");
ArrayList<String> b = new ArrayList<String>();
b.add("Hello");
b.add("Bye");
if (a.size() != b.size()) {
System.out.println("Arrays must have the same size");
return;
}
ArrayList<Boolean> results = new ArrayList<Boolean>();
for(int i = 0; i < a.size(); ++i)
results.add(a.get(i).equals(b.get(i)));
for(int i = 0; i < a.size(); ++i)
System.out.println(results.get(i));
}
}
In Java, the output of s is 0. I do not understand why and would it be possible to somehow get the correct value of s (1000 here)?
public static void main(String args) {
int s = 0;
List<Integer> list = getList(s);
System.out.println("s = " + s);
}
public static List<Integer> getList(int s) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
list.add(i); s++;
}
}
In C# there were out descriptors to indicate that the variable is going to change if I'm not mistaken..
I'm not going to get the list.size() in general!
In Java, all method arguments are passed by value, i.e. copy. So, changes to the copy are not visible to the caller.
To address your second question, you can just use list.size() on the caller side.
I see two ways
1) Make 's' as static variable and move it to class level
2) Create class with getter/setter for list and int and return the object for getList call
public static MyWrapperObj getList(int s) {
......
return wrapperObj
}
class MyWrapperObj
{
private List<Integer>;
private countS;
....
//getter/setters.
}
Java doesn't allow for passing parameters by reference - but you could wrap it in an object like this:
class IntHolder {
private int s;
IntHolder(int s){
this.s = s;
}
public void setS(int s){
this.s = s;
}
public int getS(){
return s;
}
public void increment(){
s++;
}
}
class Test{
public static void main(String[] args) {
IntHolder s = new IntHolder(0);
List<Integer> list = getList(s);
System.out.println("s = " + s.getS());
}
public static List<Integer> getList(IntHolder s) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
list.add(i); s.increment();
}
return list;
}
}
In java, arguments passed to methods are passed by value.. you will need to make s a global or instance variable in order to modify it in other methods. This is just the way java works. e.g.
public class Test{
private int s;
public Test(){
s=0;
increment();
//print now will be 1000.
}
private void increment(){
s = 1000;
}
}