Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:11:15+00:00 2026-06-15T10:11:15+00:00

Still trying to get a handle on programming in Java, below is the code

  • 0

Still trying to get a handle on programming in Java, below is the code to a recent assignment in multiple methods that I’ve already submitted for college.

My query is, is it possible to streamline the code any to make it more effective as opposed going about it via a longer route.

1: Print highest value of an Array.
2: Print lowest value of an Array.
3: Print average value of Array.
4: Print number of occurrences of a specific word in a String.
5: Print the average word length of a string.

public class MaxMinAverage {
static int[] values = {1, 4, 3, 57, 7, 14, 7, 3, 10, 5, 4, 4, 10, 5, -88};
static String sentence = "the cat sat on the mat and the dog sat on the rug";
public static void main(String[] args) {
    System.out.println("MaxMinAverage.java\n=====================");
    System.out.println("Maximum value = "+getMaximum(values));
    System.out.println("Minimum value = "+getMinimum(values));
    System.out.println("Average Value =" +getAverage(values));
    System.out.println("Frequency of 'the' = "+getFrequency(sentence,"the"));
    System.out.println("Average word length = "+getAverageWordLength(sentence));
    }
    public static int getMaximum(int[]arr){
        int max = 0;
        for(int i = 0; i < values.length; i++){
            if(values[i] > max){
                    max = values[i];
                    }
            }
        return max;
     }
    public static int getMinimum(int[] arr){
        int min = 0;
        for(int i = 1; i < values.length; i++){
            if(values[i] < min){
                    min = values[i];
                    }
            }
        return min;
    }
    public static float getAverage(int[] arr){
        float result = 0;
        for(float i = 0; i < values.length; i++){
             result = result + values[(int) i];
        }
        return result/values.length;
     }
    public static int getFrequency(String sentance, String word){
        String keyword = "the";
        String[] temp;
        String space = " ";
        temp = sentence.split(space);
        int counter = 0;
        for(int i = 0; i < temp.length; i++){
            if(temp[i].equals(keyword)){
                counter++;
            }
        }
        return counter;
    }
    public static float getAverageWordLength(String sentance){
        String characters = sentence.replaceAll("\\W","");
        float total = characters.length();
        float result = 0;
        String[] temp;
        String space = " ";
        temp = sentence.split(space);
        for(int i = 0; i < temp.length; i++){
            result++;   
        }
        return total/result;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:11:17+00:00Added an answer on June 15, 2026 at 10:11 am

    Making it more effective:

    You could always use DRY(Don’t repeat yourself) here and create a ArrayUtils class and keep all these methods there and generalize them so that you can reuse them.

    public static int getMinimum(int[] arr){
            int min = arr[0];  //change here
            for(int i = 1; i < values.length; i++){
                if(values[i] < min){
                        min = values[i];
                        }
                }
            return min;
        }
    

    similar change for max method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still trying to get the hang of SQL. I built 1 query that
Just trying to still get my head around IOC principles. Q1: Static Methods -
Sometimes I still get stuck trying to translate procedural code into functional code. Is
I'm still trying to get a handle on Cocoa (both in Obj-C and MacRuby),
I'm trying to get a handle on DDD and feel that I've got a
Trying to get a handle on pattern matching here-- coming from a C++/Java background
I'm still trying to get a hang of Perl's OOP features. I'm confused about
I'm still trying to get my feet on the ground with Silverlight and RIA
So I'm still trying to get comfortable using Parsec, but I'm slowly working it
I am a bit puzzled with my Regex results (and still trying to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.