Saturday, 7 December 2019

Write a console program that prompts the user for an integer.

Q: Mini-program: Write a console program that prompts the user for an integer. As long as the user keeps entering positive integers, continue prompting for the next integer. Once the user enters an integer of 0 or less, report the largest integer that was entered and end the program.

Solution:


import java.util.ArrayList;
import java.util.Scanner;

public class LargestNumber

{
public static void main(String args[])
{
int ending=0;
ArrayList<Integer> list=new ArrayList<Integer>();/* declare arrylist to store the value*/
int value=1;
while(value>0)
{
System.out.println("Please Enter the number");/* promt the user to give the number*/
Scanner in=new Scanner(System.in);
value=in.nextInt();/* store the entered value */
ending =value;
if(value>0){/* check if the value is greater than zero than only it will store*/
list.add(value);
}
 
}
int max=0;
for(int i=0;i<list.size();i++)
{
if(list.get(i).intValue()>max)
max=list.get(i).intValue();
 
}
System.out.println("----------------------------------------");
System.out.println("largest number is=");/* print the largest number*/
System.out.println(max);
}
}

Design and implement a reusable program that takes any standard Shape and returns a Rectangle that completely incloses the Shape.

Q: Design and implement a reusable program that takes any standard Shape and returns a Rectangle that completely incloses the Shape. I expect you to write a class that defines a method to handle a shape. Let's name the class as MyClass and the method as reusable. For simplicity, consider only Rectangle 2 cap D.Double and Line2D.Double whenever you need to show s Shapes in your answer. You don't have to concern about drawing shapes for this question. Your job is to answer the subquestions (a) through (d). API is on the last page. Identify required classe(s) and interface(s) to solve the problem and draw the corresponding class diagram to depict their relationship. Include specific Shapes in your class diagram. Implement the reusable program MyClass. Write a tester that calls the reusable method with two different specific Shape objects. public class Tester {public static void main (String [] args) {//your work goes here.}} Briefly explain how polymorphism is used in the execution of the reusable method.

Solution:
 
a) Rectangle and Circle both are Shape.
So we selected Rectangle and Circle as Classes and Shape as interface for both, becouse shape has common funtionality fot both.
Classes Rectangle is a Shape and Circle is a Shape so both has "IS A" relationship with Shape
b)Implematation =>
//================== Shape.java ===========================//
public interface Shape {
   public double getArea();
   public double perimeter();
}
//========================== Rectangle.java ===========================//
public class Rectangle implements Shape {
   private double height;
   private double length;
 
   public Rectangle() {
     
   }
   public Rectangle(double h,double l) {
       height = h;
       length = l;
   }
 
   @Override
   public double getArea() {
       return height*length;
   }
   @Override
   public double perimeter() {
       return 2.0d*(height+length);
   }
   public double getHeight() {
       return height;
   }
   public void setHeight(double height) {
       this.height = height;
   }
   public double getLength() {
       return length;
   }
   public void setLength(double length) {
       this.length = length;
   }
}
//============================= Circle.java ========================//
public class Circle implements Shape {
   private double radius;
   final private double PI = 22.0d/7.0d;
   public Circle() {
     
   }
   public Circle(double r) {
       radius = r;
   }
   @Override
   public double getArea() {
       return PI*radius*radius;
   }
 
   @Override
   public double perimeter() {
       return 2.0*PI*radius;
   }
   public double getRadius() {
       return radius;
   }
   public void setRadius(double radius) {
       this.radius = radius;
   }
   public double getPI() {
       return PI;
   }
}
c) Test Program
//============================= ReusableTest.java==================================//
public class ReusableTest {
   public static void main(String[] args) {
       Shape[] shapes = new Shape[2];
       shapes[0] = new Rectangle(40.0d, 30.0d);
       shapes[1] = new Circle(20.0d);
       for(int i = 0;i<2;i++){
           System.out.println("Shape "+i+" Area :"+shapes[i].getArea());
           System.out.println("Shape "+i+" Perimeter :"+shapes[i].perimeter());
       }
   }
}
d)
We have careated Shape refrence variable and storing the object of circle and rectangel
by using shape only we are calling method area and perimeter but actual method is called from respective objcet only, so that is the example of run time polimorphysm

Friday, 6 December 2019

What will the following code fragment printout?

What will the following code fragment printout? 
String myString = "Java": System.out.println(myString.toUppercase() + "or" + myString): 
 A. java or Java 
B. Java or Java 
C. JAVA or JAVA 
D. JAVA or Java

Solution:

 
D (JAVA or Java)

The below program has two varints the first variant which converts all characters in the string to upper case and second variant takes locale as argument and converts into upper case

Write a complete Java program for the following questions

Q. Write a complete Java program for the following questions:    
         Write a program that print the following on the screen.
Java
Java Java
Java Java Java
Java Java Java Java
Java Java Java Java Java

Solution:

 
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
   public static void main (String[] args) throws java.lang.Exception
   {
       int i,j;
       for(i=0;i<5;i++)
       {
           for(j=0;j<=i;j++)
               System.out.print("JAVA ");
             
           System.out.println();
       }
   }
}

Southridge Company incurred the following costs related to its purchase of a new assembly line

1. Southridge Company incurred the following costs related to its purchase of a new assembly line: Purchase cost: Php 9,435,000 (including Php 435,000 freight and purchase taxes)
Recoverable purchase taxes: Php 960,000
Irrecoverable purchase taxes: Php 870,000
Installation of assembly line: Php 975,000
Training of employees: Php 195,000
Estimated repair and maintenance costs over useful life: Php 500,000
What is the amount to be recognized as asset related to the assembly line?
a.
Php 11,280,000
b.
Php 9,450,000
c.
Php 9,645,000


Solution:

Answer: b) Php 9,450,000
Note:
1.As per IAS 16-Property Plant & Equipment, Employee Training cost will not be part of Assets capitalization. unless those cost is directly associated to bring assets to required condition & use as intended by management.
2. Repair & Maintenance cost can not be part assets capitalization..

Php
Cost of New assembly    9,435,000.00
Less:Recoverable Tax     (960,000.00)
Add:Installation cost        975,000.00
Asset to be Recognized 9,450,000.00

Wednesday, 4 December 2019

Write a program that has a declaration in main() to store the string "Vacation"

Q: Write a program that has a declaration in main() to store the string "Vacation" i near in an array named message. Also, add a function call to display() that accepts message in an argument named strng and then displays the contents of message by using pointer notation *(strng + i), modify the display() function to use the expressions *strng rather than *(strng + i). The program I compiled from this question is listed below, I am struggling with placements of notations on the latter part of the question.

#include <iostream>
using namespace std;

int main(){
void dispstr(char*);
char str[] = "Vacation";

display(str);
return 0;
}

void display(char* ps)
{
while( *ps )
cout << *ps++;
cout << endl;
}

Solution:

 #include <iostream>
using namespace std;

int main(){
void dispstr(char*);
char str[] = "Vacation";

display(str);
return 0;
}

void display(char* ps)
{
while( *ps )
cout << *ps++;
cout << endl;
}

Write a program that has a declaration in main() to store the string Vacation

Q: Write a program that has a declaration in main() to store the string Vacation is near in an array named message. Include a function call to display () that accepts message in an argument named string and then displays the contents of message by using the pointer notation *(strng + i).
Then modify the display() function written to use the expression *strng rather than *(strng +i) to retrieve the correct element.


Solution:

#include <iostream>

using namespace std;

void display(char* string)
{
for(int i=0; i<strlen(string); i++)
cout << *(string+i);
}

void display2(char* string)
{
int len = strlen(string);
for(int i=0; i<len; i++)
{
cout << *string;
string++;
}
}

int main()
{
char message[50] = "Vacation is near";


display(message);
cout << endl;
display2(message);
return 0;