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

Java Basics Lab Practice

The document contains 8 code snippets that demonstrate various Java concepts like data types, input/output, type casting, and calculations. The code snippets include: 1. A method to display event details by passing parameters 2. Displaying ASCII value of a character input 3. Adding integers from an array using type casting 4. Calculating area of a tile game board by multiplying sides 5. Converting Celsius to Fahrenheit 6. Demonstrating different data types by taking input 7. Calculating number of days for different stages of a trade fair 8. No code shown, described as displaying student details
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Java Basics Lab Practice

The document contains 8 code snippets that demonstrate various Java concepts like data types, input/output, type casting, and calculations. The code snippets include: 1. A method to display event details by passing parameters 2. Displaying ASCII value of a character input 3. Adding integers from an array using type casting 4. Calculating area of a tile game board by multiplying sides 5. Converting Celsius to Fahrenheit 6. Demonstrating different data types by taking input 7. Calculating number of days for different stages of a trade fair 8. No code shown, described as displaying student details
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

-----------------------------------------------------------------------------------

-------------------------------------1
import java.util.Scanner;
class Main
{
static void display(String event,String type,int count,char paid,float expense)
{
System.out.println("Event Name : "+event);
System.out.println("Event Type : "+type);
System.out.println("Expected Count : "+count);
System.out.println("Paid Entry: "+paid);
System.out.println("Projected Expense: "+expense+"L");
}
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
String event=obj.nextLine();
String type=obj.nextLine();
int count=obj.nextInt();
char paid=obj.next().charAt(0);
float expense=obj.nextFloat();
display(event,type,count,paid,expense);
}
}

olp:-
track 1
public
500
Y
56.0

Event Name : track 1


Event Type : public
Expected Count : 500
Paid Entry: Y
Projected Expense: 56.0L

-----------------------------------------------------------------------------------
-------------------------------2(display asci value in character)

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
char input=obj.next().charAt(0);
int value1=input;
if (value1>=65 && value1<=122)
{
float value2=input;
System.out.println("int:"+value1);
System.out.println("float:"+value2);
System.out.println("byte:"+value1);
System.out.println("short:"+value1);
}
else
{
System.out.println("Invalid");
}
}
}

olp:-

int:65
float:65.0
byte:65
short:65

-----------------------------------------------------------------------------------
----------------3(play with type casting)

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int nums[];
nums = new int[2];
int i,temp=0;

for (i = 0; i < nums.length; i++) {


nums[i] = sc.nextInt();
}
for (i = 0; i < nums.length; i++) {
temp=temp+nums[i];
}
double total=temp;
System.out.println(total);
}
}

olp:-

10 2
12
-----------------------------------------------------------------------------------
--------------------4(tile game)(2 test cases passed) change the approach
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
int side=obj.nextInt();
int no_tiles=obj.nextInt();
side=side+side;
int large_tile=side*side;
System.out.println(large_tile);
}
}

olp:-
5
8
100
-----------------------------------------------------------------------------------
-----5)celsius to fahreheit

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
int celsius=obj.nextInt();
double fahrenheit=(1.8*(celsius))+32;
System.out.println(fahrenheit);
}
}

olp:-

51
123.8
---------------------------------------------------------------------------------
6)basic different data types$$$$(this question it is running in vscode but not in
neocode platform)
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
int a=obj.nextInt();
double b=obj.nextDouble();
boolean c=obj.nextBoolean();
char d=obj.next().charAt(0);
Scanner obj1=new Scanner(System.in);
String f=obj1.nextLine();
System.out.println("Integer value = "+a);
System.out.println("Double value = "+b);
System.out.println("Boolean value = "+c);
System.out.println("char value = "+d);
System.out.println("String value = "+f);
}
}

olp:-

1
1.21
true
g
this ios dawsd wdwa
Integer value = 1
Double value = 1.21
Boolean value = true
char value = g
String value = this ios dawsd wdwa
-----------------------------------------------------------------------------------
-------7)Trade fair

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
int a=obj.nextInt();
int day1=(a*2)/7;
int day2=(day1*2);
int day3=(day1)/2;
System.out.println(day1);
System.out.println(day2);
System.out.println(day3);
}
}
olp:-
10500

3000
6000
1500
-----------------------------------------------------------------------------------
----------------8)display student's detail
rest are basic.

You might also like