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

Java - Util.scanner Apples (Main (String ) (Scanner Scanner (System.)

The document provides examples of different Java concepts including: 1) Importing and using the Scanner class to take user input; 2) Creating a simple calculator program using user input, arithmetic operators, and output statements; 3) Examples of if/else statements and logical operators; 4) A demonstration of switch/case statements in a calculator program.

Uploaded by

Rohan Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Java - Util.scanner Apples (Main (String ) (Scanner Scanner (System.)

The document provides examples of different Java concepts including: 1) Importing and using the Scanner class to take user input; 2) Creating a simple calculator program using user input, arithmetic operators, and output statements; 3) Examples of if/else statements and logical operators; 4) A demonstration of switch/case statements in a calculator program.

Uploaded by

Rohan Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

OUTPUT:

System.out.print("Hello World"); ------> The next output after this will be on the same line as this;

System.out.println("Hello world); ------> The next output after this will be on a new line;

INPUT:

Firstly you need to import the scanner at the top line before the class

import java.util.Scanner; ---> This imports the scanner

Scanner bucky = new Scanner (System.in); ---> This assigns the value given by user to a
variable called as bucky.

Then to take input and to store in any variable


boy = bucky.nextInt(); ---> This assigns the value user inputs to a variable 'boy'

nextInt is used because boy is 'int'

use nextLine for string and so on.

after taking input from the keyboard, inorder to print it out:

System.out.println(bucky.nextLine); ---> The nextLine is used so the program waits for the
input to be given and doesnt directly jump to output

SIMPLE ADDITION CALCULATOR:

import java.util.Scanner;

public class apples {

public static void main (String args[]) {

Scanner bucky = new Scanner (System.in);

double fnum, snum, answer;


System.out.println("Enter a number");

fnum = bucky.nextDouble();

System.out.println("Enter a number to add");

snum = bucky.nextDouble();

answer=fnum+snum;

System.out.print("The answer is " + answer);

IF ELSE SYNTAX

int test = 6;

if (test <= 9)

System.out.println("Yes");

else

System.out.println("This is else");

int test = 6;

if (test <= 9)

System.out.println("Yes");
}

else

System.out.println("This is else");

NOTE: "&&" this means that conditions on both sides must be met
inorder to print the true (if) statement.

"||" this is used as "or". ie either one condition is true the


value is true.

Example of if else syntax:

import java.util.Scanner;

public class apples {

public static void main (String args[]) {

int boy, girl;

Scanner rohan = new Scanner (System.in);

System.out.println("Enter the Age of the Boy");

boy= rohan.nextInt();

System.out.println("Enter the age of the girl");

girl = rohan.nextInt();
if(boy>=18 || girl<=50)

System.out.println("Welcome to Tinder");

else

System.out.println("FUCK OFF");

SWITCH CASE:

simple Calculator with 0 errors

import java.util.Scanner;

public class apples {

public static void main (String args[]) {

System.out.println("Which operation do you want to


perform");

System.out.println("1. Add 2. Subtract 3. Multiply 4.


Divide");

Scanner scan = new Scanner (System.in);

char op;
double fnum,snum,ans;

op = scan.next().charAt(0);

System.out.println("Please enter first number");

fnum = scan.nextDouble();

System.out.println("Please enter second number");

snum = scan.nextDouble();

switch(op)

case '1':

ans = fnum+snum;

System.out.println("The addition is "+ ans);break;

case '2':

ans = fnum - snum;

System.out.println("The subtraction is " + ans);break;

case '3':

ans = fnum * snum;

System.out.println("The multiplication is " + ans);break;

case '4':

ans = fnum/snum;

System.out.println("The division is " + ans);break;

default :

System.out.println("Madarchod jitna bola hai utne me


se select kar. Bakchodi mat kar");break;

}
}

WHILE SYNTAX:

int counter =0;

while (counter <10)

System.out.println(counter);

counter++;

MULTIPLE CLASSES:

There is a class called as tuna. (Our main class is apples)

in tuna class we write a code:

public class tuna {

public void simplemessage(String name)

System.out.println("Hello "+name);
}

There is no main funtion/ method in class tuna, we go to our main


class apples and use this class tuna as:
public class apples {

public static void main (String args[]) {

Scanner scan= new Scanner(System.in);

Sysout(“Enter Your name here: ”);

String name = scan.NextLine();

tuna tunaObj = new tuna();

tuna.simplemessage(name);

The tunaObject, it is an object. In tuna class there can be many


functions, to know which function we want to call, we use an object.
We create a new object as shown then we use the object.

You might also like