Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the full Java code for a class below that represents an apple where the fo

ID: 3860921 • Letter: W

Question

Write the full Java code for a class below that represents an apple where the following things are important to represent: Apples have a weight and a brand and cannot be created without these. Apples have a natural ordering by weight, brand is not important). When an apple is printed, it should display all the properties of the apple. In the space BELOW write the full java code for a main method. In that main method: a) Declare an array containing two instances of the apple class you made above b) Using flow control and the mechanism for the natural ordering of apples you implemented above, print out the "greater" apple of the two in the array or the word "equal" if they are equal.

Explanation / Answer

java code fpr array containing two instances of the apple class

java code for natural order of apple

import java.util.ArrayList;
import java.util.List;

class apple{
int price;

public void myFunction(int iPrice)
{
price=iPrice;
}
}

class orange{
int price;

public void myFunction(int iPrice)
{
price=iPrice;
}
}

public class main {

public static void main(String[] args) {
List <Object> list= new ArrayList<>();

//create 3 apple object to list
list.add( new apple() );
list.add( new apple() );
list.add( new orange() );

list.get(0). /* "get(0)." this isn't using apple object and my function */

}
}


=====================================================================================


import java.util.Arrays;
import java.util.Collections;

public class HashtableDemo {

public static void main(String args[]) {
String[] companies = { "Google", "Apple", "Sony" };

// sorting java array in ascending order
System.out.println("Sorting String Array in Ascending order in Java Example");
System.out.println("Unsorted String Array in Java: ");
printNumbers(companies);
Arrays.sort(companies);
System.out.println("Sorted String Array in ascending order : ");
printNumbers(companies);

// sorting java array in descending order
System.out.println("Sorting Array in Descending order in Java Example");
System.out.println("Unsorted int Array in Java: ");
printNumbers(companies);
Arrays.sort(companies, Collections.reverseOrder());
System.out.println("Sorted int Array in descending order : ");
printNumbers(companies);

System.out.println("Sorting part of array in java:");
int[] numbers = { 1, 3, 2, 5, 4 };
Arrays.sort(numbers, 0, 3);
System.out.println("Sorted sub array in Java: ");
for (int num : numbers) {
System.out.println(num);
}

}

public static void printNumbers(String[] companies) {
for (String company : companies) {
System.out.println(company);
}
}

}


Read more: http://javarevisited.blogspot.com/2012/01/sort-array-in-java-ascending-and.html#ixzz4nyJpS0sz

import java.util.Scanner;
public class Even_Odd
{
public static void main(String[] args)
{
int n, j = 0, k = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
int odd[] = new int[n];
int even[] = new int[n];
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for(int i = 0; i < n; i++)
{
if(a[i] % 2 != 0)
{
odd[j] = a[i];
j++;
}
else
{
even[k] = a[i];
k++;
}
}
System.out.print("Odd:");
if(j > 1)
{
for(int i = 0;i < (j-1); i++)
{
System.out.print(odd[i]+",");
}
System.out.print(odd[j-1]);
}
else
{
System.out.println("No number");
}
System.out.println("");
System.out.print("Even:");
if(k > 1)
{
for(int i = 0; i < (k-1); i++)
{
System.out.print(even[i]+",");
}
System.out.print(even[k-1]);
}
else
{
System.out.println("No number");
}
}