Hello, I need help for the Exam Practice 1. Given the Item class below, implemen
ID: 3861527 • Letter: H
Question
Hello, I need help for the Exam Practice
1. Given the Item class below, implement the Comparable interface so two Item objects can be compared. Two Item objects should be compared first by weight, then by height if the weights are the same.
public class Item
{
private int weight;
private int height;
// Assume constructors, gets, sets, etc. already exist.
// Write the code to compare two Items based on the directions above:
}//end class Item
2. Write the selectionSort method as it appears in your utility class: CSCD210 – Programming Principles I 3/10/2015
Eastern Washington University
3. Write the code to call selectionSort (in your utility class) for this array:
public static void main(String[] args)
{
Item[] theItems = new Item[3];
theItems[0] = new Item(12, 54);
theItems[1] = new Item(2, 42);
theItems[2] = new Item(142, 24);
// Add code to call selectionSort from your utility class
}
4. Explain ‘static’ as used in a Java program.
5. In the Java statement below, what is meant by the keyword ‘this’?
this.name = name;
6. Create a method named reverseArray that:
- accepts an array of doubles,
- reverses the contents of the array
7. Given the simple version of the Student class as presented in lectures, write the equals method and the toString method:
public class Student
{
private int studentID;
private String name;
} // end of Student class
Explanation / Answer
Answer 1 :
public class item implements Comparable
{
private int weight;
private int height;
public item(int weight, int height)
{
this.weight = weight;
this.height = height;
}
public int getWeight()
{
return this.age;
}
public int getHeight()
{
return this.name;
}
@Override
public bool compareTo(item per)
{
if (this.weight == per.weight)
if (this.height == per.weight)
return true;
else
return false;
}
public static void main(String[] args)
{
item e1 = new item(30, 45);
item e2 = new item(70, 60);
bool retval = e1.compareTo(e2);
if (reval) {
//Both are equal
}
}
}
Answer 4:
The static keyword in java is used for memory management system.We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class & instance of the class.
The static can be:
1.variable(also called as class variable)
2.method(also called as class method)
3.block
4.nested class
Answer 5:
This is a keyword in Java. It can be used inside the Method or constructor of Class.
This works as a reference to the current Object whose Method or constructor is being invoked.
The this can be used to refer to any member of the current object from within an instance Method or a constructor.
It is use to differentiate the formal parameter and data members of class, when ever the formal parameter and data members of the class are equal then jvm get dilimma(no clarity between formal parameter and member of the class)
To distinguish between formal parameter and data member of the class, the data member of the class must be preceded by "this".
Answer 6 :
Answer 7:
using System;
public class Student
{
private int studentID;
private string name;
public string toString()
{//overriding the toString() method
return studentID + " " + name;
}
Boolean equals(Object obj)
{
return (this == obj);
}
} // end of Student class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.