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

Java Programs On Data Types & Operators PDF

The document provides examples of Java programs that demonstrate various Java concepts like data types, operators, classes, objects, methods, conditionals, loops, strings, and more. A variety of programs are shown including ones that calculate volume of boxes, print factorials, greet users, simulate coin flips, and demonstrate char data types.

Uploaded by

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

Java Programs On Data Types & Operators PDF

The document provides examples of Java programs that demonstrate various Java concepts like data types, operators, classes, objects, methods, conditionals, loops, strings, and more. A variety of programs are shown including ones that calculate volume of boxes, print factorials, greet users, simulate coin flips, and demonstrate char data types.

Uploaded by

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

Java Programs on Data types & operators.

/* A C program that uses the Box structure. Call this


file boxDemop.c */
struct box {
double width;
double height;
double depth;
};
void main() {
struct box *mybox;
mybox = malloc(sizeof(struct 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;
printf("Volume is :%f ", vol);
}
/*
[vishnu@mannava ~]$ cc boxdemo.c
[vishnu@mannava ~]$ ./a.out
Volume is :3000.000000
*/

/* A Java program that uses the Box class. Call this


file BoxDemo.java */
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
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);
}
}
/*
[vishnu@mannava ~]$ javac BoxDemo.java

[vishnu@mannava ~]$ java BoxDemo


Volume is 3000.0
*/

/* Example of creating an object: Puppy.java */


public class Puppy1{
public Puppy1(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public static void main(String []args){
// Following statement would create an object myPuppy
Puppy1 myPuppy = new Puppy1( "tommy" );
}
}
/*
[vishnu@mannava ~]$ javac Puppy1.java
[vishnu@mannava ~]$ java Puppy1
Passed Name is :tommy
*/

Accessing Instance Variables and Methods:


Instance variables and methods are accessed via created objects.
/* First create an object */
ObjectReference = new Constructor();
/* Now call a variable as follows */
ObjectReference.variableName;
/* Now you can call a class method as follows */
ObjectReference.MethodName();

/* This example explains how to access instance


variables and methods of a class: Puppy.java */
public class Puppy{
int puppyAge;
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Name chosen is :" + name );
}
public void setAge( int age ){
puppyAge = age;
}
public int getAge( ){
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args){
/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );
/* Call class method to set puppy's age */
myPuppy.setAge( 2 );

Java Programs on Data types & operators.


/* Call another class method to get puppy's age */
myPuppy.getAge( );
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}
/*
[vishnu@mannava ~]$ java Puppy
Name chosen is :tommy
Puppy's age is :2
Variable Value :2
*/

/* Here, Box uses a parameterized constructor to


initialize the dimensions of a box. */
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxDemo7 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
/*
[vishnu@mannava ~]$ javac BoxDemo7.java

[vishnu@mannava ~]$ java BoxDemo7


Volume is 3000.0
Volume is 162.0
*/

/* iterative factorial Program using loop in java */


class Factorial{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
/*
[vishnu@mannava ~]$ javac Factorial.java
[vishnu@mannava ~]$ java Factorial
Factorial of 5 is: 120
*/

/* Welcome Program with messages:


WelcomeWithThreeMessages.java*/
public class WelcomeWithThreeMessages {
public static void main(String[] args) {
System.out.println("Programming is fun!");
System.out.println("Fundamentals First");
System.out.println("Problem Driven");
}
}
/*[vishnu@mannava ~]$ javac WelcomeWithThreeMessages.java
[vishnu@mannava ~]$ java WelcomeWithThreeMessages
Programming is fun!
Fundamentals First
Problem Driven
*/

/*Prints "Hi, Bob. How are you?" where "Bob" is


replaced by the command-line argument*/
public class UseArgument {
public static void main(String[] args) {
System.out.print("Hi, ");
System.out.print(args[0]);
System.out.println(". How are you?");
}
}
/*
[vishnu@mannava ~]$ javac UseArgument.java
[vishnu@mannava ~]$ java UseArgument

Java Programs on Data types & operators.


Hi, Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 0
at UseArgument.main(UseArgument.java:20)
[vishnu@mannava ~]$ java UseArgument vishnu
Hi, vishnu. How are you?
*/

/*Prints the relative lengths of the subdivisions on a


ruler.*/
public class Ruler {
public static void main(String[]
String ruler1 = " 1 ";
String ruler2 = ruler1 + "2"
String ruler3 = ruler2 + "3"
String ruler4 = ruler3 + "4"
String ruler5 = ruler4 + "5"
System.out.println(ruler1);
System.out.println(ruler2);
System.out.println(ruler3);
System.out.println(ruler4);
System.out.println(ruler5);
}
}
/*
[vishnu@mannava ~]$ javac Ruler.java
[vishnu@mannava ~]$ java Ruler
1
1 2 1
1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2
*/

args) {
+
+
+
+

ruler1;
ruler2;
ruler3;
ruler4;

1 3 1 2 1 4 1 2 1 3 1 2 1

/*Illustrates Integer.parseInt()and the integer


operations a + b, a * b, a / b, and a % b.*/
public class IntOps {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
int prod = a * b;
int quot = a / b;
int rem = a % b;
System.out.println(a + " + " + b + " = " + sum);
System.out.println(a + " * " + b + " = " + prod);
System.out.println(a + " / " + b + " = " + quot);
System.out.println(a + " % " + b + " = " + rem);
System.out.println(a + " = " + quot + " * " + b + " + " +
rem);

}
}
/*
[vishnu@mannava ~]$ javac IntOps.java
[vishnu@mannava ~]$ java IntOps 234 567
234 + 567 = 801
234 * 567 = 132678
234 / 567 = 0
234 % 567 = 234
234 = 0 * 567 + 234
*/

/* Given b and c, solves for the roots of x*x + b*x +


c. Assumes both roots are real valued. */
public class Quadratic {
public static void main(String[] args) {
double b = Double.parseDouble(args[0]);
double c = Double.parseDouble(args[1]);
double discriminant = b*b - 4.0*c;
double sqroot = Math.sqrt(discriminant);
double root1 = (-b + sqroot) / 2.0;
double root2 = (-b - sqroot) / 2.0;
System.out.println(root1);
System.out.println(root2);
}
}
/*
[vishnu@mannava ~]$ javac Quadratic.java
[vishnu@mannava ~]$ java Quadratic -3.0 2.0
2.0
1.0
*/

/* Prints true if n corresponds to a leap year, and


false otherwise. Assumes n >= 1582, corresponding to a
year in the Gregorian calendar. */
public class LeapYear {
public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
boolean isLeapYear;
// divisible by 4
isLeapYear = (year % 4 == 0);
// divisible by 4 and not 100
isLeapYear = isLeapYear && (year % 100 != 0);
// divisible by 4 and not 100 unless divisible by 400
isLeapYear = isLeapYear || (year % 400 == 0);
System.out.println(isLeapYear);
}
}

/*
[vishnu@mannava ~]$ javac LeapYear.java
[vishnu@mannava ~]$ java LeapYear 2016
True
*/

/* Prints a pseudo-random integer between 0 and N-1.


Illustrate an explicit type conversion (cast) from
double to int.*/
public class RandomInt {
public static void main(String[] args) {
// a positive integer
int n = Integer.parseInt(args[0]);
// a pseudo-random real between 0.0 and 1.0
double r = Math.random();
// a pseudo-random integer between 0 and n-1
int value = (int) (r * n);
System.out.println(value);
}
}
/*
[vishnu@mannava ~]$ javac RandomInt.java
[vishnu@mannava ~]$ java RandomInt 100
58
*/

/* Simulate a fair coin flip and print out "Heads" or


"Tails" accordingly. */
public class Flip {
public static void main(String[] args) {
// Math.random() returns a value between 0.0 and 1.0
// so it is heads or tails 50% of the time
if (Math.random() < 0.5) System.out.println("Heads");
else
System.out.println("Tails");
}
}
/*
[vishnu@mannava ~]$ javac Flip.java
[vishnu@mannava ~]$ java Flip
Heads
*/

/* Prints ith Hello for i = 1 to 10. Illlustrates using


a while loop for a repetitive task.*/
public class TenHellos {
public static void main(String[] args) {
// print out special cases whose ordinal doesn't end in th
System.out.println("1st Hello");
System.out.println("2nd Hello");
System.out.println("3rd Hello");

Java Programs on Data types & operators.


// count from i = 4 to 10
int i = 4;
while (i <= 10) {
System.out.println(i + "th Hello");
i = i + 1;
}
}
}/*
[vishnu@mannava ~]$ javac TenHellos.java
[vishnu@mannava ~]$ java TenHellos
1st Hello
2nd Hello
3rd Hello
4th Hello
5th Hello
6th Hello
7th Hello
8th Hello
9th Hello
10th Hello
*/

/* Demonstrate char data type. */


class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
[vishnu@mannava ~]$ javac CharDemo.java
[vishnu@mannava ~]$ java CharDemo
ch1 and ch2: X Y

/* char variables behave like integers. */


class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1
System.out.println("ch1 is now " + ch1);
}
}
[vishnu@mannava ~]$ javac CharDemo2.java
[vishnu@mannava ~]$java CharDemo2
ch1 contains X
ch1 is now Y

You might also like