I'm trying to make a Calculator, so I have this Calculator.java and CalculatorTest.java using setters and getters.
public class Calculator {
private int a;
public int getA() {
return a;
}
public void setA(int calcOneA) {
this.a = calcOneA;
}
private int b;
public int getB() {
return b;
}
public void setB(int calcOneB) {
this.b = calcOneB;
}
private char sign;
public char getSign() {
return sign;
}
public void setSign(char calcOneSign) {
this.sign = calcOneSign;
}
}
import java.util.Scanner;
public class CalculatorTest {
public static void main(String[] args) {
Calculator calcOne = new Calculator();
Scanner scanner = new Scanner(System.in);
System.out.println("Введите первое число");
calcOne.setA(scanner.nextInt());
System.out.println("Введите знак математической операции");
calcOne.setSign(nextLine());
System.out.println("Введите второе число");
calcOne.setB(scanner.nextInt());
}
}
I don't know how to put math sign into calcOne.setSign(). I tried nextLine() but it says can't find symbol.
Your problem isn't the setter.
For example, you can call calcOne.setSign('a'); on its own.
Now, to reproduce that for user input, you need to fix your scanner usage, for example
calcOne.setSign(scanner.nextLine().charAt(0));
However, if you're typing something like 2 + 2, the + is called the operator, and it's not always a specific index within the string (if you type this on its own line, it might be okay)
nextLine() returns a string, so you must extract the first character:
String s = scanner.nextLine();
// check that s has one and only one character.
calcOne.setSign(s.charAt(0));
Related
Hi i am trying to solve the problem I am facing
public class exam {
public static void main(String[] args) {
test1 a = new test1();
}
int zahl(int x, int y) {
int e;
if(x>y) {
e=x-y;
}else {
e=y-x;
}
if(e==0) {
return 0;
}
int z=0;
int i=1;
while(i<=e) {
z=z+i;
i++;
}
return z;
}
}
what I want to do is to call the zahl method to the test1 class
public class test1{
private exam b;
public void init() {
b = new exam();
}
void test() {
int result = b.zahl(2, 2);
assertEquals(1, result);
}
}
this is what I have tried, but it returns nothing, even though it's supposed to show me error.
You should probably be declaring your functions with the public tag i.e. public void test() if you intend to access them from other functions outside of that package. The usual Class naming convention in Java is with capital first letter, which makes your code more readable for you and others.
For your question, I don't think you are actually invoking the test() method of the test1 class. If you want that method to get called every time, you could place it inside the default Constructor.
I want to get increasing value starting with 1 so that I get TK-1 all the way to TK-n.
What I've tried is:
public class Main {
addTicket();
public void addTicket() {
int a;
String ticket;
a = getA();
ticket = "TK-"+ a
System.out.println(ticket)
}
public int getA() {
int a, b;
a = 0;
b = a++;
return a;
public static void main(String[] args) {
new Main();
}
}
Sorry it's my first time learning to code, can someone explain to me why it isn't working and what should I do to make it work ?
Thanks in advance.
If you need an autoincrease ID, you need a static int:
public class Ticket {
static int ticketID = 1;
public void addTicket() {
int a;
String ticket;
a = getticketID();
ticket = "TK-" + a;
System.out.println(ticket);
}
public int getticketID () {
return ticketID++;
}
public static void main(String[] args) {
Ticket test = new Ticket();
test.addTicket();
test.addTicket();
test.addTicket();
}
}
output:
enter image description here
can someone take a look at this example and tell me why I get null10 as printed value instead of 10 only?
and is there and easier solution for this program without using global String variable "word"
public class UserInput {
public static String word;
public static class TextInput {
public void add(char c) {
word = word + c;
}
public static String getValue() {
return word;
}
}
public static class NumericInput extends TextInput {
#Override
public void add(char c) {
if (Character.isDigit(c)){
word = word + c;
}
}
}
public static void main(String[] args) {
TextInput input = new NumericInput();
input.add('1');
input.add('a');
input.add('0');
System.out.println(input.getValue());
}
}
EDIT: I need use inherits from TextInput
You need to give your static word field an initial value, otherwise it will default to being null. And when Java concatenates String objects, it will treat a null reference as the literal string "null". So you're effectively always starting off with the String "null".
If you give your class field a starting value of "" (empty string) then your code should do what you expect.
With regard to a better way of doing this, I would instead give the class a non-static field of type StringBuilder (and initialise it so that it's not null). Then your add method can simply append(c) the new characters to the StringBuilder field object, which will be more efficient than repeatedly using string concatenation (which is what you get with word + c).
You are not initializing input, so it is null. You need to initialize input first in order to make concatenating work.
So, use this:
public static String word = "";
Rather than using a static variable that is shared over all instances and children of the TextInput class, you should be using an instance variable.
You'll still have to initialize a non null value
That would look like
public static class TextInput {
protected String word;
public TextInput() {
this.word = "";
}
public void add(char c) {
word = word + c;
}
public String getValue() {
return word;
}
}
To better understand the problem, try your code with this
TextInput input = new TextInput();
input.add('a');
System.out.println(input.getValue());
TextInput input2 = new NumericInput();
input2.add('1');
input2.add('0');
System.out.println(input2.getValue());
Additional, see #Bobulous comment about using StringBuilder
You were not initializing the "word".
public class TextInput {
public static String word=""; // a lil change here
public static class TextInput {
public void add(char c) {
word += c;
}
public String getValue() {
return word;
}
}
public static class NumericInput extends TextInput {
public void add(char c) {
if (Character.isDigit(c)){
word += c;
}
}
}
public static void main(String[] args) {
NumericInput input = new NumericInput();
input.add('1');
input.add('a');
input.add('0');
System.out.print(input.getValue());
}
}
Hello So I have a entire class called tractor with different data's stored in it but now I'm suppose to create an object call tractor with a zero parameter constructor but This is the code I have so far and its giving em errors
First off this my Tractor Class which is in a different file:
import java.util.Scanner;
class Tractor
{
private int RentalRate;
private int RentalDays;
private int VehicleID;
private int RentalProfit;
public void setRentalRate(int r)
{
Scanner input = new Scanner(System.in);
System.out.println("What's the Rental Rate?");
int num = input.nextInt();
num = r;
if(r<0 || r >1000)
RentalRate = r;
RentalRate= 1;
}
public int getRentalRate()
{
return RentalRate;
}
public void setVehicleID(int v)
{
Scanner input = new Scanner(System.in);
System.out.println("What's the vehicleID?");
int num1 = input.nextInt();
num1 = v;
if(v<0)
VehicleID = v;
VehicleID = 1;
}
public int getVehicleID()
{
return VehicleID;
}
public void setRentalDays(int d)
{
Scanner input = new Scanner(System.in);
System.out.println("How many rental days?");
int num2 = input.nextInt();
num2 = d;
if(d<0)
RentalDays = d;
RentalDays = 1;
}
public int getRentalDays()
{
return RentalDays;
}
public String toString()
{
String str;
str = "RentalDays:" + RentalDays +"\nRenalRate:" + RentalRate + "\nVehicleID " + VehicleID;
return str;
}
public void RentalProfit(int RentalRate, int RentalDays)
{
RentalProfit = RentalRate * RentalDays;
}
}
import java.util.Scanner;
public class testTractor
{
public static void main(String[] args)
{
public tractor()
{
this.RentalDays = d;
this.RentalRate = r;
this.VehicleID = v;
}
}
}
The error is :
testTractor.java:7: error: illegal start of expression
public tractor()
^
testTractor.java:7: error: ';' expected
public tractor()
^
2 errors
You have compilation errors. You need to first declare the Tractor class then add the constructor inside it. One way to do is declare in a separate file. Also in Java unless you had defined d you couldnt have assigned it. Maybe you wanted to assign the day as a String look in the examples I provide below.
You need to to first create a file call Tractor.java and then define variables there. For example contents of Tractor.java:
public class Tractor {
String rentaldays,someOtherValue;
public Tractor(){
rentaldays ="monday";
someOtherValue="value";
}
//or
public Tractor(String rentalDays){
this.rentaldays = rentalDays;
someOtherValue = "asf";
}
}
Then in your main method You can do Tractor trac = new Tractor(); or Tractor trac = new Tractor("tuesday"); also after that you can print the rentaldays of trac using System.out.println(trac.rentaldays);
From the looks of it you will probably be making a tractor rental system. In that case, rentalDays may be an array of Strings. And then you would have an array of Tractor objects to store in the rental system. You can look at these terms and keywords to point you in the right direction.
You are defining it wrong, define your methods inside class then call them in main() method.
class Test{
public void greeting(){
System.out.print("hello to JAVA..");
}
public static void main(String[] args){
Test testObj = new Test();
testObj.greeting();
}
}
you use an illegal of java syntax, if you already have class tractor in your project. for calling it to in other class, try below code
public class TestTractor(){
Tractor objTractor;
public static void main(String[] args){
//create new tractor object with no parameter
objTractor = new Tractor();
//create new tractor object with parameter
objTractor = new Tractor(parameter here);
//do some action of object here
...........
}
}
//This is just a sample
in your tractor class add below code
public tractor()
{
this.RentalDays = d;
this.RentalRate = r;
this.VehicleID = v;
}
And keep your TestTractor class as
public class TestTractor(){
public static void main(String[] args){
Tractor objTractor = new Tractor();
// objTractor.yourMethodName
}
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
Now if I have to increment the variable... which one is the way of doing ?
variables.setA(variables.getA()+1);
in this way a always is 1.
can i solve the problem?
That code is correct (if verbose). The following:
public class Main {
private int a = 0;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public static void main(String[] args)
{
Main variables = new Main();
System.out.println(variables.getA());
variables.setA(variables.getA()+1);
System.out.println(variables.getA());
variables.setA(variables.getA()+1);
System.out.println(variables.getA());
variables.setA(variables.getA()+1);
System.out.println(variables.getA());
}
}
prints
0
1
2
3
Given the verbosity of variables.setA(variables.getA()+1), you might want to wrap it into a method (e.g. incrementA() or addToA(int) etc)
Your way of incrementing a variable should work, but you could also define a new method:
public void incrementA(){
a++;
}