ujjwal
Functions and Methods
5 Answer(s)      4 years and 11 months ago
Posted in : Java Beginners

(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.

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);
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);
String str=input.nextLine();
lower(str);
}
}
```

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
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
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
What is functions?
What is functions?
What is functions?
What is functions?  Hi, Please tell me about PHP functions? Thanks in advance.   About Functions: Functions in PHP is block of code... functions and use it multiple times. In PHP there is many pre-defined functions
Inbuild functions
Inbuild functions  List all the inbuild functions in java
PHP Files management functions and examples.
language provides many functions for the manipulation of files from the PHP program. Here in this section we will learn about all the PHP file functions.
static functions
2. Static Functions 3. Instance Variable  Static Variables Static... object. Static Methods Static method is the method which belongs to the class... call only other static methods and can not call a non-static method from it. 3
Methods of HttpServlet
Methods of HttpServlet  What are methods of HttpServlet
PHP Functions, PHP Functions Tutorials
PHP Functions In this section we will understand important PHP functions with the help of example code. What is Functions? Function is a block of code...; } Functions in PHP PHP provides many built-in functions to do the real job
functions strstr() and stristr().
functions strstr() and stristr().  What is the functionality of the functions strstr() and stristr
Boolean functions in JavaScript
Boolean functions in JavaScript  Explain about the Boolean functions present in Javascript
the functions unlink and unset  What is the difference between the functions unlink and unset
decodeURI() and encodeURI() functions in JavaScript
decodeURI() and encodeURI() functions in JavaScript  What are decodeURI() and encodeURI() functions in JavaScript
What are encryption functions in PHP?
What are encryption functions in PHP?  What are encryption functions in PHP
Even and odd functions in java
Even and odd functions in java  Even and odd functions in java  ... have two separate functions for checking either number is even or odd. We are passing number as argument in methods
Boolean functions present in Javascript
Boolean functions present in Javascript   Explain about the Boolean functions present in Javascript
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
php array functions
php array functions  php array functions list
php stream functions
php stream functions  what does php stream functions do
What is Character Functions
What is Character Functions  What is Character Functions   Hi, Here is the answer, Character Functions are INITCAP, UPPER, LOWER, SUBSTR & LENGTH. Additional functions are GREATEST & LEAST. Group Functions
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
c-language pointer functions
c-language pointer functions  what is the execution process of character pointer functions with example