IJVM

Ads

 
 
 

Share on Google+Share on Google+

charles kaifiti
IJVM
1 Answer(s)      6 years ago
Posted in : Java Beginners

need help

The ‚??power‚?? function currently does not exist in IJVM. For example, if two numbers 2 and 3 are on the stack, the power function would compute 2 power 3, i.e. 23 that should give 8 as the result. Assume two positive numbers (say, a and b) are provided on the stack (with a being the first value and b being the value on top). Write an IJVM code segment that will calculate the power of the two numbers (ab) and put the result back on the stack. You are allowed to use existing IJVM instructions only. Include appropriate comments to explain your programm output

Ads
View Answers

May 30, 2011 at 2:44 PM


import java.util.*;

public class Calculate {
    public static int power(int a, int b) {
int answer = 1; 
for(int i = 0; i < b; i++) { 
answer *= a;
}
return answer; 
}

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Num1: ");
        int num1 = input.nextInt();
        System.out.print("Num2: ");
        int num2 = input.nextInt();
        if (num1 > 0 && num2 > 0) {
            int sum = num1 + num2;
            System.out.println("Sum= " + sum);
            int diff = 0;
            if (num1 > num2) {
                diff = num1 - num2;
            } else {
                diff = num2 - num1;
            }
            System.out.println("Difference= " + diff);
            int prod = num1 * num2;
            System.out.println("Product= " + prod);
            int pow = Calculate.power(num1,num2);
            System.out.println("power= " + pow);
        } else {
            System.out.println("Invalid Numbers");
            System.out.println("End of program");
        }
    }
}

Ads









Related Tutorials/Questions & Answers:
IJVM
in IJVM. For example, if two numbers 2 and 3 are on the stack, the power function... value and b being the value on top). Write an IJVM code segment... are allowed to use existing IJVM instructions only. Include appropriate comments
IJVM
in IJVM. For example, if two numbers 2 and 3 are on the stack, the power function... value and b being the value on top). Write an IJVM code segment... are allowed to use existing IJVM instructions only. Include appropriate comments
Advertisements
IJVM
IJVM  using IJVM..can anybody help me in this question User should be prompted to enter two non-negative numbers (say, a and b where a >0 and b > 0) line by line and then press the enter key. Your program should
Tutorials   
Java Spring Hibernate Struts Training ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java

Ads

 
Advertisement null

Ads