Archive for the ‘Uncategorized’ Category

POLYMORPHISM

MAIN CLASS

public class Main
{

public static void main(String[] args)
{

// Declare and create Product array of size 5
Product[]product = new Product[3];

product[0] = new Book(250, “UP Press”, 2001);
product[1] = new MP3Player(5000, “Apple”, “silver”);
product[2] = new TV(50000, “National Panasonic”, 20);

System.out.println(“”);
System.out.println(“”);

for(int index= 0; index Polymorphic Behavior”);
System.out.println(“”);

double totalSalePrice = 0;

//Polymorphic Behavior
for(int index= 0; index < product.length; index++)
{
totalSalePrice+=product[index].computeSalePrice();
}
System.out.println("totalSalePrice = " + totalSalePrice);
}

}
==============================================================================
PRODUCT CLASS

public class Product
{

private double regularPrice;

/** Creates a new instance of Product */
public Product(double regularPrice)
{
this.regularPrice = regularPrice;
}

// Method that will be overridden
public double computeSalePrice()
{
return 0;
}

public double getRegularPrice()
{
return regularPrice;
}

public void setRegularPrice(double regularPrice)
{
this.regularPrice = regularPrice;
}

}

==============================================================================
ELECTRONICS CLASS

public class Electronics extends Product{

private String manufacturer;

/** Creates a new instance of Electronics */
public Electronics(double regularPrice, String manufacturer)
{
super(regularPrice);
this.manufacturer = manufacturer;
}

// Override this method
public double computeSalePrice()
{
return super.getRegularPrice() * 0.6;
}

public String getManufacturer()
{
return manufacturer;
}

public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

}
==============================================================================
BOOK CLASS
public class Book extends Product
{

private String publisher;
private int yearPublished;

/** Creates a new instance of Book */
public Book(double regularPrice, String publisher, int yearPublished)
{
super(regularPrice);
this.publisher = publisher;
this.yearPublished = yearPublished;
}

// Override this method
public double computeSalePrice()
{
return super.getRegularPrice() * 0.5;
}

public String getPublisher()
{
return publisher;
}

public void setPublisher(String publisher)
{
this.publisher = publisher;
}

public int getYearPublished()
{
return yearPublished;
}

public void setYearPublished(int yearPublished)
{
this.yearPublished = yearPublished;
}

}

==============================================================================
TV CLASS

public class TV extends Electronics
{

int size;

/** Creates a new instance of TV */
public TV(double regularPrice, String manufacturer, int size)
{
super(regularPrice, manufacturer);
this.size = size;

}

// Override this method
public double computeSalePrice()
{
return super.getRegularPrice() * 0.8;
}
}
==============================================================================
MP3PLAYER CLSS

public class MP3Player extends Electronics
{

private String color;

/**
* Creates a new instance of MP3Player
*/
public MP3Player(double regularPrice, String manufacturer, String color)
{
super(regularPrice, manufacturer);
this.color = color;
}

// Override this method
public double computeSalePrice()
{
return super.getRegularPrice() * 0.9;
}

public String getColor()
{
return color;
}

public void setColor(String color)
{
this.color=color;
}
}

==============================================================================

Accessing Instance Variable in a Static Class

public class VariableTypes{
static String staticVariable = “this is static”;

String instanceVariable;
VariableTypes(){
instanceVariable = “this is an instance variable”;
}

public static void main(String []args){

String localVariable = “this is a local variable”;

VariableTypes insVar = new VariableTypes();

System.out.println(“staticVariable: ” + staticVariable);
System.out.println(“instanceVariable: ” + insVar.instanceVariable);
System.out.println(“localVariable: ” + localVariable);
}
}

Assignment

ASSIGNMENT
Set 1.

a. Modify ObjectAndClass.java
b. Create another object instance of Long class using new keyword.
c. Display it using System.out.println().
d. Compile and run.

Set 2.
a. Compile StaticAndInstance.java
b. Kindly the describe the problem with the code.
c. Explain each statement in the code (line by line).

Set 3.
a. Compile VariableTypes.java.
b. how can you resolve the issue with the instance variable.
c. Submit the correct code.

CreateName

public class CreateName {

public static void main(String[] args) {

if (args.length == 0) {
System.out.println(“No Command Line arguments”);
}
else {
System.out.println(“You provided ” + args.length+ ” arguments”);
for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "]: "+ args[i]);
}
System.out.print("The new word generated is: ");
System.out.print(generateNewName(args));
System.out.println("\n");
}
}

public static String generateNewName(String[] str){
String letter="";

for (int i = 0; i < str.length; i++) {
letter=letter + str[i].charAt(1);
}
return letter;
}
}

Getting Input, Conditional Constructs, Array, Command Line

Using Command-line, write a program the counts the number of vowels supplied in the command line as parameters. It must determine the count of each vowel separately. The character count is not case sensitive.
Use String class, charAt() method.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CommandLineArg {
public static void main(String args[]) throws IOException{
BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Enter a word: “);
String word = dataIn.readLine();
countVowel(word);
}

public static void countVowel(String text){
int count = 0;
for (int i = 0; i < text.length(); i++) {
char vowel = text.charAt(i);
if (vowel=='a' || vowel=='e' || vowel=='i'
|| vowel=='o' || vowel=='u') {
count++;
}
}
System.out.println("'"+ text + "'"+" has " + count + " vowels");
}
}

Getting Input, Conditional Constructs, Array, Command Line

Using BufferedReader and JoptionPane, ask for 10 numbers from the user. Use an array to store the values of these 10 numbers. Output on the screen the number with the greatest value.

//BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Exercise5A {

public static void main(String[] args) {
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
int[] num = new int[10];
int counter;
int max = 0;
int totalnumber = 10;

for(counter = 0; counter max)){
max = num[counter];
}
}

System.out.println(“\nThe number with the greatest value is ” + max);
}

}

//JoptionPane
import javax.swing.JOptionPane;

public class Exercise5B {

public static void main(String[] args) {
int[] num = new int[10];
int counter;
int max = 0;
int totalnumber = 10;

for(counter = 0; counter max)){
max = num[counter];
}
}

JOptionPane.showMessageDialog(null,”The number with the greatest value is ” + max);
}

}

Getting Input, Conditional Constructs, Array, Command Line

Get three exam grades from the user and compute the average of the grades. Output the average of the three exams. Together with the average, also include a smiley face : ) in the output if the average is greater than or equal to 60, otherwise : ( .
Use BufferedReader and JoptionPane to get input from the user and output the result.

//BufferedReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Exercise4A {

public static void main(String[] args){
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
float grade1=0;
float grade2=0;
float grade3=0;

try{
System.out.print(“Enter grade in 1st exam: “);
grade1= Float.parseFloat(dataIn.readLine());

System.out.print(“Enter grade in 2nd exam: “);
grade2= Float.parseFloat(dataIn.readLine());

System.out.print(“Enter grade in 3rd exam: “);
grade3= Float.parseFloat(dataIn.readLine());
}
catch(IOException e){
System.out.println(“Error.”);
}

float ave= (grade1+grade2+grade3)/3;
float round = Round(ave,2);
if(ave>=60){
System.out.println(“\nAverage: “+round+” :)”);
}
else{
System.out.println(“\nAverage: “+round+” :(“);
}
}

public static float Round(float Rval, int Rpl) {
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}
}

//JOptionPane

import javax.swing.JOptionPane;

public class Exercise4B {

public static void main(String[] args){

float grade1=0;
float grade2=0;
float grade3=0;

grade1 = Float.parseFloat
(JOptionPane.showInputDialog(“Enter grade in 1st exam: “));
grade2= Float.parseFloat
(JOptionPane.showInputDialog(“Enter grade in 2nd exam: “));
grade3 = Float.parseFloat
(JOptionPane.showInputDialog(“Enter grade in 3rd exam: “));

float ave= (grade1+grade2+grade3)/3;
float round = Round(ave,2);

if(ave>=60){
JOptionPane.showMessageDialog(null,”Average: “+round+” :)”);
}
else{
JOptionPane.showMessageDialog(null,”Average: “+round+” :(“);
}
}

public static float Round(float Rval, int Rpl) {
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}

}

Follow

Get every new post delivered to your Inbox.