can some help me make these changes to my code? make my identifiers user-friendl
ID: 3647360 • Letter: C
Question
can some help me make these changes to my code?make my identifiers user-friendly: better descriptive words.
declare something other than s as null in Scanner.
make a standard way of coding the Scanner line, there will not need to user a Scanner line twice.
get rid pf ob. in front of the function assigned to res.
Remove the three lines that begins with public class arithmetic.
declare the two identifiers in the class at the beginning off the class, with a constructor.
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Owner
*/
public class arithmtic {
private int num1;
private int num2;
Scanner s = null;
public static void main(String[] args) {
int res,exit = 1;
math ob = new math();
do{
ob.promt();
System.out.println("Select a operation to perform on the two numbers:");
System.out.println("1. Add");
System.out.println("2. subtract");
System.out.println("3. multiply");
System.out.println("4. divide");
System.out.println("5. modulo");
System.out.println("6. exit");
res = ob.s.nextInt();
switch(res){
case 1:
ob.add();
break;
case 2:
ob.subtract();
break;
case 3:
ob.multiply();
break;
case 4:
ob.divide();
break;
case 5:
ob.modulo();
break;
case 6:
System.out.println("Thanks for using my program , sybil");
exit = 0;
}
}while(exit == 1);
}
public math(){
s = new Scanner(System.in);
}
public void promt(){
System.out.print("Enter First num");
num1 = s.nextInt();
System.out.print("Enter Second num");
num2 = s.nextInt();
}
public int add(){
return num1 + num2;
}
public int subtract(){
return num1 - num2;
}
public int multiply(){
return num1 * num2;
}
public int divide(){
return num1 / num2;
}
public int modulo(){
return num1 % num2;
}
}
Explanation / Answer
Classes are generally declared using the keyword class, with the following format: class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names; Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers. All is very similar to the declaration on data structures, except that we can now include also functions and members, but also this new thing called access specifier. An access specifier is one of the following three keywords: private, public or protected. // example: class constructor #include using namespace std; class CRectangle { int width, height; public: CRectangle (int,int); int area () {return (width*height);} }; CRectangle::CRectangle (int a, int b) { width = a; height = b; } int main () { CRectangle rect (3,4); CRectangle rectb (5,6); coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.