Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

import java.util.

*;
public class Methods
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int val = sc.nextInt();
sc.nextLine(); // clear newline from input buffer
drawLine(val);
triangle(8);
System.out.println("Enter an e-mail address: ");
String address = sc.nextLine();
String hostname = getHost(address);
System.out.println("Hostname: " + hostname);
int v = 11;
int q = doSomething(v);
System.out.println(v);
System.out.println(q);
System.out.println("Here is a random number: " + getRandomNumber
());
}
public static void drawLine (int x)
{
for (int i = 0; i < x; i = i + 1)
{
System.out.print("*");
}
System.out.println(); // go to next line
}
// method that uses another method (drawLine()) to draw a right triangle
public static void triangle (int n)
{
for (int i = 1; i <= n; i = i + 1)
{
drawLine(i);
}
}
public static String getHost (String a)
{
// Start by extracting username and @ symbol
int atSign = a.indexOf("@");
// get only part after @ symbol
String noUser = a.substring(atSign + 1, a.length());
// Find first period
int dot = noUser.indexOf(".");

String host = noUser.substring(0, dot);


return host;
}
public static int doSomething (int x)
{
x = x * 5;
int y = x / 3;
return y;
}
// no input, but returns a value
public static int getRandomNumber ()
{
Random r = new Random();
return r.nextInt(10) + 5; // value between 5 and 14
}
}

You might also like