Functions and Methods

Functions and Methods

(1) Write a program in java to input 10 numbers. Use a function to find and print the cube of each number. (2) Write a program in java to input day number. Use a function daysofweek(int dysno) to accept the day number from the main class. The function uses a switch-case statement to print the corresponding day of the week. (3) Write a Java program to input a number. Use a function int Armstrong(int n) to receive the number. The function returns 1 if the number is armstrong otherwise 0. (4) Write a java program to accept a string. Pass it to a function freq(String x),the function finds and prints the frequency of each vowel. (5) Write a program in Java to input a string using upper and lower case characters. Pass the string to a function lower(String x)to print only lower case letters.

View Answers

May 16, 2012 at 4:58 PM

The given code accepts 10 numbers from the user and stored into array. Then it find the cubes of the input numbers and display it on console.

import java.util.*;

class CubeOfNumbers{
    public static void findCube(int num[]){
        for(int i=0;i<num.length;i++){
            System.out.println("Cube of "+num[i]+" is: "+(num[i]*num[i]*num[i]));
        }
    }

    public static void main(String args[]){
    Scanner input=new Scanner(System.in);
    int array[]=new int[10];
    for(int i=0;i<array.length;i++){
        array[i]=input.nextInt();
    }
    findCube(array);

    }
}

May 16, 2012 at 5:10 PM

The given code accepts the number from the user and determine the day using switch case statement.

import java.util.*;

class FindDay 
{
    public static void daysofweek(int dysno){
        switch(dysno){
            case 1:
                System.out.println("Sunday");
                break;
            case 2:
                System.out.println("Monday");
                break;
            case 3:
                System.out.println("Tuesday");
                break;
            case 4:
                System.out.println("Wednesday");
                break;
            case 5:
                System.out.println("Thursday");
                break;
            case 6:
                System.out.println("Friday");
                break;
            case 7:
                System.out.println("Saturday");
                break;
        }
    }


    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter day number (eg. 1 for sunday): ");
        int no=input.nextInt();
        daysofweek(no);

    }
}

May 16, 2012 at 5:20 PM

An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. The given code accepts the number from the user and check whether it is Armstrong number or not.

import java.util.*;

class  CheckArmstrong
{
public static int Armstrong(int num){
    int i = num;
int number=0,remainder;
while(num > 0){
remainder = num % 10;
number = number + (int)Math.pow(remainder,3);
num = num / 10;
}
if(number == i){
return 1;
}
else{
return 0;
}

}
public static void main(String[] args)  {
System.out.print("Enter number to check:");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
System.out.println(Armstrong(num));
}
}

May 16, 2012 at 5:34 PM

**The given code accept a string and pass it to a function freq(String x),the function finds and prints the frequency of each vowel.**

import java.util.*;
class FrequencyOfVowels
{
    public static void freq(String x){
         String st=x.replaceAll(" ", "");

          char[]third =st.toCharArray();
          for(int counter =0;counter<third.length;counter++){
          char ch= third[counter];
          int count=0;
          for ( int i=0; i<third.length; i++){
          if (ch==third[i])
          count++;
        }
        boolean flag=false;
        for(int j=counter-1;j>=0;j--){
        if(ch==third[j])
        flag=true;
        }
        if(!flag){
            if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){
        System.out.println(ch+" occurs "+count+" times ");
            }
        }
        }
    }
    public static void main(String[] args) 
    {
          Scanner input=new Scanner(System.in);
          System.out.print("Please enter string: ");
          String str=input.nextLine();
          freq(str);
    }
}

May 16, 2012 at 5:40 PM

The given code accepts the string pass it to function. the function then displays the accepted string into lowercase.

import java.util.*;

class StringToLowercase 
{
    public static void lower(String x){
    String str=x.toLowerCase();
    System.out.println(str);
    }
    public static void main(String[] args) 
    {
          Scanner input=new Scanner(System.in);
          System.out.print("Please enter string: ");
          String str=input.nextLine();
          lower(str);
    }
}









Related Tutorials/Questions & Answers:
Functions and Methods
Functions and Methods  Write a Java program to input the sides of a triangle. Pass the sides to a function decide(int x,int y,int z) which checks and prints whether the triangle is equilateral,isosceles or scalene.   
Functions and Methods
Functions and Methods  (1) Write a program in java to input 10 numbers. Use a function to find and print the cube of each number. (2) Write a program in java to input day number. Use a function daysofweek(int dysno) to accept
Advertisements
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
Methods of HttpServlet
Methods of HttpServlet  What are methods of HttpServlet
PHP list class methods
Function get_class_methods gives the all methods names of the given class. It takes input as class name and returns the array of the methods name PHP LIst Class Methods Example <?php class myclass{ function aa
static methods
static methods  why static methods cannot read or write the instance variables
Agile methods
Agile methods   Why use Agile methods?   This methods focus on shorter iterations, in which the software is brought to a releasable level of quality fairly often, usually somewhere between weekly and monthly. Short
native methods
native methods  what is native methods in java?   A native method is a method that is implemented in a language other than Java. The Java native method is a great way to gain and merge the power of C or C++ programming
validate() and reset() methods
validate() and reset() methods   Describe validate() and reset() methods
Various methods of httpservletresponse interface
Various methods of httpservletresponse interface  What are the various methods of httpservletresponse interface
methods type - Java Beginners
methods type in Java  Give me an example programs of methods types in Java
ModuleNotFoundError: No module named 'methods'
ModuleNotFoundError: No module named 'methods'  Hi, My Python... 'methods' How to remove the ModuleNotFoundError: No module named 'methods... to install padas library. You can install methods python with following command
Cookie methods in jsp
Cookie methods in jsp   Define cookie methods in jsp ?    Cookie methods : clone() getComment() getDomain() getMaxAge() getName() getPath() getSecure() getValue() getSecure() getVersion
java object class methods
java object class methods  What are the methods in Object class?  There are lots of methods in object class. the list of some methods are as- clone equals wait finalize getClass hashCode notify notifyAll
GET and POST methods
GET and POST methods   What are the differences between GET and POST methods in form submitting, give the case where we can use GET and we can use POST methods
PHP Magic Methods
can not use the magic methods name in any user-defined functions...Magic Methods in PHP: In PHP there are so many methods are included in recent versions, magic methods are one of them. The magic methods are listed below
Java overloaded methods
Java overloaded methods  Can overloaded methods can also be overridden
factory methods in java?
factory methods in java?  what are factory methods in java?   Hi Friend, Factory methods are static methods that return an instance of the native class like Pattern.compile(), Calendar.getInstance
Final Methods - Java Tutorials
_TO_REPLACE_1 final methods The final method  can be declare as follows: public
Servlet Methods
Servlet Methods In this section we will read about the various methods... methods that are used to initialize a Servlet, handles the request received..., the Servlet methods are called life-cycle methods of Servlet. Following
abstract methods in java
abstract methods in java  what is abstract methods in java.give better examples for understanding   Hi Friend, Please visit the following link: http://www.roseindia.net/java/master-java/abstract-class.shtml Thanks
to create a java class and methods
it with methods that can be used to reverse a list & append two lists.Also to comment on whether the dsign ade has led to make methods for append
Methods in Jsp - Development process
Methods in Jsp   Hi, My interviewer said we should declare & define all methods inside _jspService() method only. is it correct . Thanks Prakash  Hi Friend, Yes, all the methods should be declared and defined
Callback Methods
Callback Methods       Callbacks methods are the way of managing life cycle of an instance. Callback methods are generally used by containers. The methods are called at specific time
looping through a list of methods
looping through a list of methods  I have a number of methods that do the almost same thing: public void setColorRed (int colorvalue); public...); Is there a way to place these methods in a list and then call each one from
methods in the applet - Applet
methods in the applet  import java.awt.*; import java.applet.Applet... provides the Applet class with default implementation for all the applet methods. You can implement these methods in the applet class when you want to override
What are weak methods?
What are weak methods?  Hi, I am beginner in Data Science... methods? Try to provide me good examples or tutorials links so that I can learn the topic "What are weak methods?". Also tell me which is the good
Overloaded methods - Java Beginners
Overloaded methods  Write two overloaded methods that return the average of an array with the following headers: a) public static int average(int[] array) b) public static double average(double[] array)   Hi Friend
HTML form methods GET and POST
HTML form methods GET and POST  What is the difference between the HTML form methods GET and POST
The ActionForm and what are important methods in ActionForm.
The ActionForm and what are important methods in ActionForm.  What is the ActionForm and what are important methods in ActionForm
bean life cycle methods in spring?
bean life cycle methods in spring?  bean life cycle methods in spring
What modifiers are allowed for methods in an Interface?
What modifiers are allowed for methods in an Interface?   Hi, What modifiers are allowed for methods in an Interface? thanks
list of predeined methods and use java
list of predeined methods and use java  I need list of predefined methods in java like reverse,compare,tostring, etc
what is class methods in objective c
what is class methods in objective c  What is class methods in objective c? Explain the class method of objective c with the help of an example
what is class methods in objective c
what is class methods in objective c  What is class methods in objective c? Explain the class method of objective c with the help of an example
An application using swings and vector methods
An application using swings and vector methods   Hi, I want an application in Java swings which uses good selection of Vectors methods
Methods in Java - Java Beginners
Methods in Java  Hello. Currently i am involved in a group project and we have each been given a specific part of code to create our joint programI however have been given the job to create a method for storing and recalling 5
PHP Files management functions and examples.
language provides many functions for the manipulation of files from the PHP
ModuleNotFoundError: No module named 'prioritized_methods'
ModuleNotFoundError: No module named 'prioritized_methods'  Hi, My... named 'prioritized_methods' How to remove the ModuleNotFoundError: No module named 'prioritized_methods' error? Thanks   Hi
ModuleNotFoundError: No module named 'uproot-methods'
ModuleNotFoundError: No module named 'uproot-methods'  Hi, My... named 'uproot-methods' How to remove the ModuleNotFoundError: No module named 'uproot-methods' error? Thanks   Hi, In your python
What are the methods in Object? - Java Beginners
What are the methods in Object?   Hi, What are the methods in Object? Give example of methods in Object. Thanks   Hello, There are lot of method in object class some of are as follows toString(); wait
some methods not working on firefox and chrome
some methods not working on firefox and chrome  why some of javascript methods are not working on firefox and chrome? ex : createElement(). Please help me to solve this problem. adv thanx
Java :Thread Methods
Java :Thread Methods This section explains methods of Thread class. Thread Methods : Thread class provides many method to handle different thread... the temporal executing thread object.ADS_TO_REPLACE_4 Some other methods
AsyncContext Interface important methods
In this section , you will get to know about important methods of AsyncContext Interface
AsyncListener Interface important methods
In this section, you will get to know about important methods AsyncListener Interface
Class AsyncEvent important methods
In this section, you will learn about the important methods of AsyncEvent Class
java file with many methods - Ajax
java file with many methods  I have to send response to a java file where there are many methods and I have to call one of them by passing parameter .How can I do
RIAs Methods And Techniques
RIAs Methods And Techniques JavaScript It is the first major client side language technology that has the ability to run code and installed on several major of web clients. Earlier its uses were relatively limited but the development

Ads