0% found this document useful (0 votes)
28 views7 pages

Lab 1

This document contains code snippets demonstrating various Java basics including: 1) Printing "Hello" and "hi there" in the main method of a class. 2) Adding two integers and printing the sum. 3) A program that takes integer and floating point input from the user and prints them. 4) A program that takes two integers as input from the user and prints their sum. 5) Code with operators, relational operators, and conditional logic that prints outputs.

Uploaded by

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

Lab 1

This document contains code snippets demonstrating various Java basics including: 1) Printing "Hello" and "hi there" in the main method of a class. 2) Adding two integers and printing the sum. 3) A program that takes integer and floating point input from the user and prints them. 4) A program that takes two integers as input from the user and prints their sum. 5) Code with operators, relational operators, and conditional logic that prints outputs.

Uploaded by

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

Java Basics

Getting Started
public class hello {
public static void main(String[] arg) {
System.out.println("Hello");
System.out.println("hi there");
}
}
Adding Two Integers
public class Add{
public static void main(String args[])
{
int x=6,y=7;
int sum=x+y;
System.out.println("x+ y ="+sum);
}
}
Write a java program that adds two integers taken from the user
import java.util.*;
public class Input {
public static void main(String args[])
{ int num1; double num2; String name; double sum;
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer: ");
num1 = in.nextInt();
System.out.print("Enter a floating point number: ");
num2 = in.nextDouble();
System.out.print("Enter your name: ");
name = in.next();
System.out.println("Name:"+name);
System.out.println("Integer:"+num1);
System.out.println("Real:"+num2);
} }
Adding two integers taken from the user
import java.util.*;
public class add {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number:");
int x=in.nextInt();
System.out.print("Enter the second number:");
int y=in.nextInt();
int sum=x+y;
System.out.print(“x+y="+sum);
}}
What is the output of the following code
public class Operators {
public static void main(String[] args) {
int number = 5*3+6/3+9-3;
System.out.println(number);
int x = 7,y = 2;
System.out.println(x/y+"\t"+ x%2);
int i=2,j=3;
int f=++i * j--;
System.out.println(f);
}
}
What are the values of r and a ?
public class Relational {
public static void main(String[] args) {
int x = 8, y = 2;
boolean r = (15 == x + y);
System.out.println(r);
boolean a = ((x>5) && (y!=2)|| (y<= (x+y)));
System.out.println(a);
}
}

You might also like