Friday, March 20, 2015

Print Pass Fail By Scanner And Continuous Using Method

Program:

package passfailproject;

import java.util.Scanner;

public class Checkpassfail {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
int inputMark;
System.out.println("Enter -1 to exit the program");
System.out.println("Enter Your mark that you have got");
inputMark=sc.nextInt();
while(inputMark!=-1){
yourresult(inputMark);
System.out.println("Enter your mark again");
inputMark=sc.nextInt();
}
System.out.println("You have exited the program");
}
static public void yourresult(int mark) {

if(mark<50){
System.out.println("You have Failed");
}else{
System.out.println("You Have Passed");
}
}

}

Output:

Enter -1 to exit the program
Enter Your mark that you have got
60
You Have Passed
Enter your mark again
56
You Have Passed
Enter your mark again
49
You have Failed
Enter your mark again
76
You Have Passed
Enter your mark again
-1
You have exited the program

Screenshots

Program



Output


Monday, March 2, 2015

Object and Classes::public,private,default,constructor method,instance method In Java

Program:

package newfifthlecture;

public class Learnnewclass {
public int aNewnumber=12;//example
static public Carclass myCar;//creating new object
static public Carclass myanothercarCar;//creating another new object
public static void main(String[] args) {

myCar=new Carclass();//calling
System.out.println("Car manufacturer name: "+ myCar.getManufacturerName());
myanothercarCar=new Carclass("TOYOTA","Corolla", 230);//calling and initialize
System.out.println("modelname of my car: "+myanothercarCar.modelName);
}
}

New class in same package:

package newfifthlecture;

public class Carclass {
private String manufacturerName;
public String modelName;

private double fuelAmount;

public Carclass() {
//constructor method
manufacturerName=new String();
modelName="Allion";
fuelAmount=30;
}
public Carclass(String manu,String model,double fuel) {
//another constructor method
manufacturerName=manu;
modelName=model;
fuelAmount=fuel;
}
public String getManufacturerName(){
// instance method
return manufacturerName;
}
}

Output:

Car manufacturer name:
modelname of my car: Corolla

Screenshots

program 1:


program 2:


output


Sunday, March 1, 2015

Project Two for Java: Pascal's Triangle

Program:

package fourthlecture;

import java.util.Scanner;

public class Pascaltriangle {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter -1 to exit ");
System.out.println("Please give a number which you convert pascal triangle: ");
int yourchoice=sc.nextInt();
while(yourchoice !=-1){
if(yourchoice <-1 || yourchoice>20){
System.out.println("Please input a value between 1~20 ");
yourchoice=sc.nextInt();
continue;
}
pascaltrianglee(yourchoice);
System.out.println("\n\n");
System.out.println("Please input a value between 1~20 ");
yourchoice=sc.nextInt();

}

System.out.println("You have exited the program.");

}

static void pascaltrianglee(int length){
int[][] pascal = new int[length][length];
for(int i=0;i<length;i++){
pascal[0][i]=1;
pascal[i][0]=1;
}


for(int row=1;row<length;row++){
for(int column=1;column<length-row;column++){
pascal[row][column]=pascal[row][column-1]+pascal[row-1][column];
}
}


for(int row=0;row<length;row++){
for(int column=0;column<length-row;column++){
System.out.print(pascal[row][column]+"\t");
}

System.out.println("\n");
}
}
}

Output:

Enter -1 to exit
Please give a number which you convert pascal triangle:
5
1 1 1 1 1

1 2 3 4

1 3 6

1 4

1




Please input a value between 1~20
6
1 1 1 1 1 1

1 2 3 4 5

1 3 6 10

1 4 10

1 5

1




Please input a value between 1~20
-1
You have exited the program.


Screenshots

program 1


Program:2


Output