I am trying to print the method findAverage in the main method, can anyone tell me how to fix - methods

public class Grade {
private int [] array = {2,3,1,4,5,7,1};
public int findSum() {
int sum;
sum = 0;
for(int i =0; i <array.length; i++)
{
sum = sum +array[i];
}
return sum;
}
public double findAverage() {
double average;
average = findSum()/array.length;
return average;
}
}
class ExamClient {
public static void main(String[] args) {
double answer;
answer = findAverage();
System.out.println("Average of all elements in the array is" + answer);
}
}

In the main you have to create a instance of the class
Create instance
public static void main(String[] args)
{
double answer;
Grade g= new Grade();
answer = g.findAverage();
System.out.println("Average of all elements in the array is" + answer);
}
Also you can make the method static

Related

Counting sort iterating from start

I have seen other questions on SO asking why the last iteration in counting sort, where we place elements on the sorted array cannot start from the start. The reason is that that way the sort algorithm loses its stability.
But what if we reversed the count also? Instead of counting the no of elements present before a specific element, what if we count the no of elements present after that specific element? I have implemented it like the following.
public class TestCountSort {
public static void main(String[] args) {
Element[] arr=new Element[]{new Element("One",1),new Element("Three",2),new Element("Two",1)};
System.out.println("Array before - "+Arrays.toString(arr));
countSort(arr);
System.out.println("Array after - "+Arrays.toString(arr));
}
public static void countSort(Element[] arr){
int n=arr.length;
int max=arr[0].key;
int min=arr[0].key;
for(Element i:arr){
if(i.key>max){
max=i.key;
}
if(i.key<min){
min=i.key;
}
}
int range=max-min+1;
int[] count=new int[range];
Element[] sortedArray=new Element[n];
for(Element i:arr){
count[i.key-min]++;
}
for(int i=range-2;i>=0;i--){
count[i]=count[i]+count[i+1];
}
for(int i=0;i<n;i++){
sortedArray[n-count[arr[i].key-min]]=arr[i];
count[arr[i].key-min]--;
}
for(int i=0;i<n;i++){
arr[i]=sortedArray[i];
}
}
}
class Element{
private String name;
public int key;
public Element(String name, int key){
this.name=name;
this.key=key;
}
public String toString(){
return "{"+name+":"+key+"}";
}
}
Will this preserve the stability and provide sorting? Or is there something I am missing?

Blank return value( sum of two numbers)

public class Sum {
public static void main(String[] args) {
add(19, 21);
}
public static int add(int number1, int number2) {
int sum = number1 + number2;
return sum;
}
}
Why is not returning the value 40 from the sum and is there a better way to write this method.

Why is my code constantly giving the same random numbers

Whenever I execute the program it always prints out 9 and 5 for sc1 and sc2. It was my understanding that the random class was supposed to be mostly random.
Here is my code:
public class BlackJack {
public static BlackJack blackjack;
public int chips;
public static int[] deck;
public static int ct = 0, sc1, sc2;
Random random;
public BlackJack() {
deck();
deal();
System.out.println(sc1);
System.out.println(sc2);
}
public void deck() {
deck = new int[52];
for (int i = 0; i < 52; i++) {
if(i % 4 == 0) {
ct++;
}
deck[i] = ct;
}
}
public void deal() {
random = new Random(52);
sc1 = deck[random.nextInt(52)];
sc2 = deck[random.nextInt(52)];
}
public static void main(String[] args) {
blackjack = new BlackJack();
}
}
Thanks in advance for any guidance.
Never mind. i'm dumb. It was the fact that i had random = new Random(52); instead of random = new Random();

Why different result each run?

I'm playing around with CompletableFuture and streams in Java 8 and I get different printouts each time I run this. Just curious, why?
public class DoIt {
public static class My {
private long cur = 0;
public long next() {
return cur++;
}
}
public static long add() {
long sum = 0;
for (long i=0; i<=100;i++) {
sum += i;
}
return sum;
}
public static long getResult(CompletableFuture<Long> f) {
long l = 0;
try {
f.complete(42l);
l = f.get();
System.out.println(l);
} catch (Exception e) {
//...
}
return l;
}
public static void main(String[] args){
ExecutorService exec = Executors.newFixedThreadPool(2);
My my = new My();
long sum = Stream.generate(my::next).limit(20000).
map(x -> CompletableFuture.supplyAsync(()-> add(), exec)).
mapToLong(f->getResult(f)).sum();
System.out.println(sum);
exec.shutdown();
}
}
If I skip the f.complete(42l) call I always get the same result.
http://download.java.net/jdk8/docs/api/java/util/concurrent/CompletableFuture.html#complete-T-
by the time the complete(42l) call happens, some add()'s may have already completed.

Storm Trident 'average aggregator

I am a newbie to Trident and I'm looking to create an 'Average' aggregator similar to 'Sum(), but for 'Average'.The following does not work:
public class Average implements CombinerAggregator<Long>.......{
public Long init(TridentTuple tuple)
{
(Long)tuple.getValue(0);
}
public Long Combine(long val1,long val2){
return val1+val2/2;
}
public Long zero(){
return 0L;
}
}
It may not be exactly syntactically correct, but that's the idea. Please help if you can. Given 2 tuples with values [2,4,1] and [2,2,5] and fields 'a','b' and 'c' and doing an average on field 'b' should return '3'. I'm not entirely sure how init() and zero() work.
Thank you so much for your help in advance.
Eli
public class Average implements CombinerAggregator<Number> {
int count = 0;
double sum = 0;
#Override
public Double init(final TridentTuple tuple) {
this.count++;
if (!(tuple.getValue(0) instanceof Double)) {
double d = ((Number) tuple.getValue(0)).doubleValue();
this.sum += d;
return d;
}
this.sum += (Double) tuple.getValue(0);
return (Double) tuple.getValue(0);
}
#Override
public Double combine(final Number val1, final Number val2) {
return this.sum / this.count;
}
#Override
public Double zero() {
this.sum = 0;
this.count = 0;
return 0D;
}
}
I am a complete newbie when it comes to Trident as well, and so I'm not entirely if the following will work. But it might:
public class AvgAgg extends BaseAggregator<AvgState> {
static class AvgState {
long count = 0;
long total = 0;
double getAverage() {
return total/count;
}
}
public AvgState init(Object batchId, TridentCollector collector) {
return new AvgState();
}
public void aggregate(AvgState state, TridentTuple tuple, TridentCollector collector) {
state.count++;
state.total++;
}
public void complete(AvgState state, TridentCollector collector) {
collector.emit(new Values(state.getAverage()));
}
}

Resources