Compile
javac file.java
javac Hello.java
Execute
java filename
java Hello
--------------------------------------------------------------------------------------
The content of the script Hello.java
public class Hello{
public static void main(String []args){
System.out.println("Hello");
}
}
#################################
//Creation of objects from a Class (java file name is Kitty.java)
//package practice;
public class Kitty{
public Kitty(String name){
// Constructor having one parameter i.e. name.
System.out.println("Given Pet Name is :" + name );
}
public static void main(String []args){
// Following statement creates objects myKitty1, myKitty2
Kitty myKitty1 = new Kitty( "Toy" );
Kitty myKitty2 = new Kitty( "Beast" );
}
}
-----------------------------------------------
Given Pet Name is :Toy
Given Pet Name is :Beast
javac file.java
javac Hello.java
Execute
java filename
java Hello
--------------------------------------------------------------------------------------
The content of the script Hello.java
public class Hello{
public static void main(String []args){
System.out.println("Hello");
}
}
#################################
//Creation of objects from a Class (java file name is Kitty.java)
//package practice;
public class Kitty{
public Kitty(String name){
// Constructor having one parameter i.e. name.
System.out.println("Given Pet Name is :" + name );
}
public static void main(String []args){
// Following statement creates objects myKitty1, myKitty2
Kitty myKitty1 = new Kitty( "Toy" );
Kitty myKitty2 = new Kitty( "Beast" );
}
}
-----------------------------------------------
Given Pet Name is :Toy
Given Pet Name is :Beast
#################################
//File name is Employee.java
package practice;
import java.io.*;
public class Employee{
String name;
int age;
String designation;
String city;
double salary;
// This is the constructor of the class Employee
public Employee(String name){
this.name = name;
}
// Assign the age of the Employee to the variable age.
public void empAge(int empAge){
age = empAge;
}
/* Assign the designation to the variable designation.*/
public void empDesignation(String empDesig){
designation = empDesig;
}
/* Assign city to the variable designation.*/
public void empCity(String empCity){
city = empCity;
}
/* Assign the salary to the variable salary.*/
public void empSalary(double empSalary){
salary = empSalary;
}
/* Print the Employee details */
public void printEmployee(){
System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" + designation );
System.out.println("City:" + city );
System.out.println("Salary:" + salary);
}
}
//File name is EmployeeTest.java
package practice;
import java.io.*;
public class EmployeeTest{
public static void main(String args[]){
/* Create two objects using constructor */
Employee empOne = new Employee("Carl");
Employee empTwo = new Employee("Rhea");
Employee empThree = new Employee("Jonas");
// Invoking methods for each object created
System.out.println("##################");
empOne.empAge(26);
empOne.empDesignation("Travel agent");
empOne.empCity("Paris");
empOne.empSalary(700);
empOne.printEmployee();
System.out.println("##################");
empTwo.empAge(35);
empTwo.empDesignation("Doctor");
empTwo.empCity("Phoenix");
empTwo.empSalary(500);
empTwo.printEmployee();
System.out.println("##################");
empThree.empAge(48);
empThree.empDesignation("Artist");
empThree.empCity("Chicago");
empThree.empSalary(200);
empThree.printEmployee();
}
}
-----------------------------------------------
##################
Name:Carl
Age:26
Designation:Travel agent
City:Paris
Salary:700.0
##################
Name:Rhea
Age:35
Designation:Doctor
City:Phoenix
Salary:500.0
##################
Name:Jonas
Age:48
Designation:Artist
City:Chicago
Salary:200.0
package practice;
import java.io.*;
public class Employee{
String name;
int age;
String designation;
String city;
double salary;
// This is the constructor of the class Employee
public Employee(String name){
this.name = name;
}
// Assign the age of the Employee to the variable age.
public void empAge(int empAge){
age = empAge;
}
/* Assign the designation to the variable designation.*/
public void empDesignation(String empDesig){
designation = empDesig;
}
/* Assign city to the variable designation.*/
public void empCity(String empCity){
city = empCity;
}
/* Assign the salary to the variable salary.*/
public void empSalary(double empSalary){
salary = empSalary;
}
/* Print the Employee details */
public void printEmployee(){
System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" + designation );
System.out.println("City:" + city );
System.out.println("Salary:" + salary);
}
}
//File name is EmployeeTest.java
package practice;
import java.io.*;
public class EmployeeTest{
public static void main(String args[]){
/* Create two objects using constructor */
Employee empOne = new Employee("Carl");
Employee empTwo = new Employee("Rhea");
Employee empThree = new Employee("Jonas");
// Invoking methods for each object created
System.out.println("##################");
empOne.empAge(26);
empOne.empDesignation("Travel agent");
empOne.empCity("Paris");
empOne.empSalary(700);
empOne.printEmployee();
System.out.println("##################");
empTwo.empAge(35);
empTwo.empDesignation("Doctor");
empTwo.empCity("Phoenix");
empTwo.empSalary(500);
empTwo.printEmployee();
System.out.println("##################");
empThree.empAge(48);
empThree.empDesignation("Artist");
empThree.empCity("Chicago");
empThree.empSalary(200);
empThree.printEmployee();
}
}
-----------------------------------------------
##################
Name:Carl
Age:26
Designation:Travel agent
City:Paris
Salary:700.0
##################
Name:Rhea
Age:35
Designation:Doctor
City:Phoenix
Salary:500.0
##################
Name:Jonas
Age:48
Designation:Artist
City:Chicago
Salary:200.0
#################################
//Reduce variable number using keyword enum
//Script fle name is PizzaTest.java (the class name, not the super class name)
package practice;
class Pizza {
enum PizzaPortion{ SMALL, MEDIUM, LARGE , VERY_LARGE}
PizzaPortion portion;
}
public class PizzaTest {
public static void main(String args[]){
Pizza slice = new Pizza();
slice.portion = Pizza.PizzaPortion.VERY_LARGE ;
System.out.println("Portion: " + slice.portion);
}
}
-----------------------------------------------
Portion: VERY_LARGE
#################################
//Accessing instance variables and methods
//File name is Toy.java
package practice;
public class Toy{
int toyAge;
public Toy(String name){
// Constructor with one parameter, name.
System.out.println("My toy dinosaur's name is:" + name );
}
public void setAge( int age ){
toyAge = age;
}
public int getAge( ){
System.out.println("It's age is " + toyAge );
return toyAge;
}
public static void main(String []args){
/* Object creation */
Toy myToy = new Toy( "Rex" );
/* Call class method to set age */
myToy.setAge( 12 );
/* Call another class method to get age */
myToy.getAge( );
/* Accessing instance variable*/
System.out.println("I bought it " + myToy.toyAge + " years back" );
System.out.println("Its cost was $10." );
}
}
-----------------------------------------------
My toy dinosaur's name is:Rex
It's age is 12
I bought it 12 years back
Its cost was $10.
#################################
//Script fle name is PizzaTest.java (the class name, not the super class name)
package practice;
class Pizza {
enum PizzaPortion{ SMALL, MEDIUM, LARGE , VERY_LARGE}
PizzaPortion portion;
}
public class PizzaTest {
public static void main(String args[]){
Pizza slice = new Pizza();
slice.portion = Pizza.PizzaPortion.VERY_LARGE ;
System.out.println("Portion: " + slice.portion);
}
}
-----------------------------------------------
Portion: VERY_LARGE
#################################
//Accessing instance variables and methods
//File name is Toy.java
package practice;
public class Toy{
int toyAge;
public Toy(String name){
// Constructor with one parameter, name.
System.out.println("My toy dinosaur's name is:" + name );
}
public void setAge( int age ){
toyAge = age;
}
public int getAge( ){
System.out.println("It's age is " + toyAge );
return toyAge;
}
public static void main(String []args){
/* Object creation */
Toy myToy = new Toy( "Rex" );
/* Call class method to set age */
myToy.setAge( 12 );
/* Call another class method to get age */
myToy.getAge( );
/* Accessing instance variable*/
System.out.println("I bought it " + myToy.toyAge + " years back" );
System.out.println("Its cost was $10." );
}
}
-----------------------------------------------
My toy dinosaur's name is:Rex
It's age is 12
I bought it 12 years back
Its cost was $10.
#################################
No comments:
Post a Comment