Hello, I need answers for all those questions : • Define “class”. Define “object
ID: 3811994 • Letter: H
Question
Hello, I need answers for all those questions :
• Define “class”. Define “object”.
• What Java code is required for a class to properly implement the Comparable interface?
• What is the output of the code segment below (show answer in space to right)?
double a = 0;
while (a <= 10)
{
System.out.print (a + “-“);
a--;
}
Write a method that called createArray that is passed a single integer value (guaranteed to be greater than 0). The method should create an array of type int, fill it with values starting at 1 (so element 0 will have a value of 1, element 1 will have 2, etc.), then return the array.
Write the 6 standard methods every self-respecting class should have for the Song class. Here’s the driver class:
public class SongTester
{
public static void main(String args[])
{
private Song[] songs;
songs = new Song[3];
songs[0] = new Song(); //defaults to: “Untitled” for name of song, and
// “Undetermined” for name of artist…
songs[1] = new Song(“I Can’t Stop Loving You”, “Ray Charles”);
songs[2] = new Song(“Daylight”, “Matt & Kim”);
System.out.println(songs[0]);
SortSearchUtil.insertionSort(songs);
System.out.println(“After sorting”);
System.out.println(songs[0]); // Is the first element the right value?
if (SortSearchUtil.linearSearch(songs, “Chain Gang”))
{
System.out.println(“Already on file.”);
}
else
{
System.out.println(“Not on file.”);
}
}// end method
}//end class SongTester
• Which is more efficient for an array of elements:
insertionSort or selectionSort..? Why?
linearSearch or binarySearch…? Why?
• What condition must be met for binarySearch to work?
• What is the time-complexity formula for binarySearch?
Thank you.
Explanation / Answer
Define class and object
Class : when we wrap the data and function in to a saingle unit we called it a class. Class is a group of objects that have similar properties. it is a blueprint from which object are created.
Object: Enitity that has state and behaviour are called objects
a object has following properties:
state: represents data (value) of an object.
behavior: represents the behavior of an object they are often referred as member functions.
Java code to implement comparable interface
the below code will create studentRecord class and will sort the data of the different student records on the basis of the age of the student
import java.util.*;
import java.io.*;
public class StudentRecordTest{
public static void main(String args[]){
ArrayList<StudentRecord> studList=new ArrayList<StudentRecord>();
studList.add(new StudentRecord("John",23,101));
studList.add(new StudentRecord("Johhny",27,106));
studList.add(new StudentRecord("Jai",21,100));
Collections.sort(studList);
for(StudentRecord stud:studList){
System.out.println(studList.rollno+" "+studList.name+" "+studList.age);
}
}
}
class StudentRecord implements Comparable<StudentRecord>{
String Fname;
int rollno;
int age;
StudentRecord(String name,int age,int rollno){
this.rollno=rollno;
this.Fname=name;
this.age=age;
}
public int compareTo(StudentRecord stud){
if(age==stud.age)
return 0;
else if(age>stud.age)
return 1;
else
return -1;
}
}
What is the output of the code segment below (show answer in space to right)?
double a = 0;
while (a <= 10)
{
System.out.print (a + “-“);
a--;
}
the avove code will go into infinte loop since the value of a is 0 initially and in the while loop value of a is decremented by 1 and is checked whether a<=0 so this condition will always be true
ans loop will never end
Write a method that called createArray that is passed a single integer value (guaranteed to be greater than 0). The method should create an array of type int, fill it with values starting at 1 (so element 0 will have a value of 1, element 1 will have 2, etc.), then return the array.
java function
int[] createArray(int arr[], int n){
for(int i=0;i<arr.length;i++){
arr[i] = n;
n++;
}
return arr;
}
full java code to test the function
public class array {
public static void main(String args[]){
int arr1[] = new int[10];
arr1 = createArray(arr1,1);
for(int i=0;i<arr1.length;i++)
System.out.print(arr1[i]+" ");
}
static int[] createArray(int arr[], int n){
for(int i=0;i<arr.length;i++){
arr[i] = n;
n++;
}
return arr;
}
}
due to time contraint i am not able to answer the other ques
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.