0% found this document useful (0 votes)
2 views

Java Lab 14

The document outlines a Java lab assignment focused on creating methods. It includes instructions for setting up a Java file and implementing three specific methods: multiplyTwo() for multiplying integers, largerString() for comparing string lengths, and writeToFile() for writing text to a file. Additionally, it provides guidance on method creation, structure, and calling methods in Java.

Uploaded by

arya.santosh2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Lab 14

The document outlines a Java lab assignment focused on creating methods. It includes instructions for setting up a Java file and implementing three specific methods: multiplyTwo() for multiplying integers, largerString() for comparing string lengths, and writeToFile() for writing text to a file. Additionally, it provides guidance on method creation, structure, and calling methods in Java.

Uploaded by

arya.santosh2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Lab 14 – Creating Methods

 Create a new Java file (empty Java file), save it to your H: drive, call it lab14
 Insert the following code to set up your program:
 Be sure to copy the test text files to the location where you saved your Java file
import java.io.*;

public class lab14 {

public static void main (String args[]) throws IOException{

100pt:

Write methods for the following problems:

 A method called multiplyTwo() that takes two integers and multiplies them together, returning
the result as an integer.
o Example: multiplyTwo(2,10) returns 20.
 A method called largerString() that takes two Strings as arguments, and returns the longer of
the two Strings.
o Example: largerString(“Hello”,”one”) returns “Hello”
 A method called writeToFile() that takes two arguments. Both arguments are Strings. The first
argument is a piece of text, the second argument is the name of a text file to store that text. The
method will take the first argument and store it in the filename given in the second argument.
There is no data returned from the method. (use void)
o Example: writeToFile(“Hello World”, “output.txt”) writes the String “Hello World” to the
text file “output.txt”.
Notes:

Creating a method:

 Determine what the arguments are, and what is returned.

Methods follow the format:

public static [return type] [method name](Arguments){


// insert method code here
// if needed, return
}

An example method to print out an integer:

public static void printInt(int x){ // void indicates there is no return


System.out.println(x);
}

An example method to “double” a String:

public static String doubleStr(String s){ // notice the String return


String twice = s + s;
return s; // return statement is required
}

Calling a method:

If it returns a value, store it:

String doubledString = doubleStr(“Hello”);

If it doesn’t return a value, you can simply execute it:

printInt(10);

You might also like