Ch1 Program's Jav

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

Prg 1

import java.util.Scanner;

public class Main

public static void main(String[] args)

// Declare the object and initialize with

// predefined standard input object

Scanner sc = new Scanner(System.in);

// String input

System.out.println("Enter name");

String name = sc.nextLine();

System.out.println("Enter gender");

// Character input

char gender = sc.next().charAt(0);

// Numerical data input

// byte, short and float can be read

// using similar-named functions.

System.out.println("Enter age");

int age = sc.nextInt();

System.out.println("Enter mobile no");

long mobileNo = sc.nextLong();


System.out.println("Enter cgpa");

double cgpa = sc.nextDouble();

// Print the values to check if the input was

// correctly obtained.

System.out.println("Name: " + name);

System.out.println("Gender: " + gender);

System.out.println("Age: " + age);

System.out.println("Mobile Number: " + mobileNo);

System.out.println("CGPA: " + cgpa);

-------

Prg 2

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main

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

String name;

BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter your name: ");


name = reader.readLine();

System.out.println("Enter your age: ");

int age = Integer.parseInt(reader.readLine());

System.out.println("Enter your Id: ");

int id = Integer.parseInt(reader.readLine());

System.out.println("Name: "+name);

System.out.println("Age: "+age);

System.out.println("Id: "+id);

-------

Prg 3

public class Main

public static void main(String[] args) {

for(int i=0; i<args.length; i++)

System.out.println("args[" + i + "]: " +

args[i]);

-----

Prg 4

import java .util.*;

public class Main

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

String ip=" I love java\n"+ "I like coding\n";

Scanner sc=new Scanner(ip);

/*System.out.println(sc.next());

System.out.println(sc.next());

System.out.println(sc.next());

System.out.println(sc.next());*/

System.out.println(sc.nextLine());

System.out.println(sc.nextLine());

Prg 5

class Box

double width;

double height;

double depth;

// This class declares an object of type Box.

class Main {

public static void main(String args[]) {

Box mybox = new Box();


double vol;

// assign values to mybox's instance variables

mybox.width = 10;

mybox.height = 20;

mybox.depth = 15;

// compute volume of box

vol = mybox.width * mybox.height * mybox.depth;

System.out.println("Volume is " + vol);

class Box {

double width;

double height;

double depth;

class Main{

public static void main(String args[]) {

Box mybox1 = new Box();

Box mybox2 = new Box();

double vol;

// assign values to mybox1's instance variables

mybox1.width = 10;

mybox1.height = 20;

mybox1.depth = 15;

/* assign different values to mybox2's


instance variables */

mybox2.width = 3;

mybox2.height = 6;

mybox2.depth = 9;

// compute volume of first box

vol = mybox1.width * mybox1.height * mybox1.depth;

System.out.println("Volume is " + vol);

// compute volume of second box

vol = mybox2.width * mybox2.height * mybox2.depth;

System.out.println("Volume is " + vol);

Prg 7

class Box {

double width;

double height;

double depth;

class Main{

public static void main(String args[]) {

Box mybox1 = new Box();

Box mybox2 = new Box();

double vol;

// assign values to mybox1's instance variables

mybox1.width = 10;
mybox1.height = 20;

mybox1.depth = 15;

/* assign different values to mybox2's

instance variables */

mybox2.width = 3;

mybox2.height = 6;

mybox2.depth = 9;

// compute volume of first box

vol = mybox1.width * mybox1.height * mybox1.depth;

System.out.println("Volume is " + vol);

// compute volume of second box

vol = mybox2.width * mybox2.height * mybox2.depth;

System.out.println("Volume is " + vol);

You might also like