The Student Room Group

Calculator with subprogram?

Hello..! I need help with my task in programming in Java. Urgent..!
Please give some feedback..

Write a program that works like an ordinary calculator. User
can use it adds, subtracts, multiplies, divides, and square roots, until you choose
exit from the program. The user also has the option of "reset" (ie. deletion
intermediate results).
Each of the operations calculator must be implemented in its method of
file that does not contain the main program.

Do not use the following methods in class String
- Contains (...), endsWith (...), indexOf (...), lastIndexOf (...)
- RegionMatches (...), replace (...), replaceAll (...), replaceFirst (...)
- Split (...), startswith (...), subsequence (...) and substring (...).
and do not use the methods of class Arrays.

here is what ihave done so far but it doesn't work...
The main program:

import java.util.Scanner;
import java.io.IOException;

public class nal {

@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) throws IOException {

int st1 = 0;
int st2 = 0;
int izbira = 6;
int i;
int p;

Scanner vnos = new Scanner(System.in);

System.out.println("Glavni meni:");
System.out.println("--------------\n");
System.out.println("1. Seštevanje");
System.out.println("2. Odštevanje");
System.out.println("3. Množenje");
System.out.println("4. Deljenje");
System.out.println("5. Kvadriranje");
System.out.println("6. Korenjenje");
System.out.println("9. Ponastavi");
System.out.println("0. Izhod\n");
System.out.println("Vpiši število operacije: ");

izbira = vnos.nextInt();

int rezultat = 0;
if (izbira == '1' || izbira == '2' || izbira == '3' || izbira == '4') {

System.out.println("Vnesi prvo stevilo: ");
st1 = vnos.nextInt();

System.out.println("Vnesi drugo stevilo: ");
st2 = vnos.nextInt();

if (izbira == '1') {
KalkulatorskaKnjiznica.metoda1(st1, st2);
System.out.println("Sestevek je: "
+ KalkulatorskaKnjiznica.metoda1(rezultat, rezultat));

}

if (izbira == '2') {
KalkulatorskaKnjiznica.metoda2(st1, st2);
System.out.println("Razlika je: "
+ KalkulatorskaKnjiznica.metoda2(rezultat, rezultat));
}
if (izbira == '3') {
KalkulatorskaKnjiznica.metoda3(st1, st2);
System.out.println("Zmnozek je: "
+ KalkulatorskaKnjiznica.metoda3(rezultat, rezultat));
}
if (izbira == '4') {
KalkulatorskaKnjiznica.metoda4(st1, st2);
System.out.println("Kolicnik je: "
+ KalkulatorskaKnjiznica.metoda4(rezultat, rezultat));
}

}

else if (izbira == '5' || izbira == '6') {

System.out.println("Vnesi stevilo: ");
st1 = vnos.nextInt();

if (izbira == '5') {
KalkulatorskaKnjiznica.metoda5(st1);
System.out.printf("Kvadrat stevila je: "
+ KalkulatorskaKnjiznica.metoda5(rezultat));
}
if (izbira == '6') {
KalkulatorskaKnjiznica.metoda6((double) st1);
double k11 = vnos.nextDouble();
System.out.printf("Koren stevila je: "
+ KalkulatorskaKnjiznica.metoda6(rezultat));
}

} else {
System.out.printf("Izbrali ste napačen znak!"); // če vnese napačen
// znak
}

if (izbira == '9' || izbira == '0') {

while (izbira == 9) {
KalkulatorskaKnjiznica.metoda9();
System.out.println("Ponastavitev.");
}
if (izbira == 0) {
KalkulatorskaKnjiznica.metoda0();
System.out.println("Izhod.");

}

}

}
}

and the subprogram:

import java.lang.Math;
import java.util.Scanner;

class KalkulatorskaKnjiznica {


public static int metoda1(int st1, int st2) {
int rezultat;
rezultat = st1 + st2;

return rezultat;

}

public static int metoda2(int st1, int st2) {

int rezultat;
rezultat = st1 - st2;

return rezultat;
}

public static int metoda3(int st1, int st2) {

int rezultat;
rezultat = st1 * st2;

return rezultat;
}

public static int metoda4(int st1 , int st2) {

int rezultat;
rezultat = (st1 / st2);

return rezultat;
}

public static int metoda5(int st1) {

int rezultat;
rezultat = st1 * st1;

return rezultat;
}

public static int metoda6(double k1){


int rezultat;
rezultat = (int) Math.sqrt(k1);

return rezultat;

}

public static void metoda9(){

@SuppressWarnings("resource")
Scanner vnos = new Scanner (System.in);
System.out.println("Vnesi stevilo 9, za ponastavitev: ");
@SuppressWarnings("unused")
int p = vnos.nextInt();




}

public static void metoda0(){
@SuppressWarnings("resource")
Scanner vnos = new Scanner (System.in);
System.out.println("Vnesi stevilo 0, za izhod: ");
int i = vnos.nextInt();
System.exit(i);

}

}
Hi there,

While you're waiting for an answer, did you know we have 300,000 study resources that could answer your question in TSR's Learn together section?

We have everything from Teacher Marked Essays to Mindmaps and Quizzes to help you with your work. Take a look around.

If you're stuck on how to get started, try creating some resources. It's free to do and can help breakdown tough topics into manageable chunks. Get creating now.

Thanks!

Not sure what all of this is about? Head here to find out more.

Quick Reply

Latest