Class attributes | Sololearn: Learn to code for FREE!

0

Class attributes

Why does the code is returning to me 0 or Null values??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String firstName = read.nextLine(); String secondName = read.nextLine(); int age = read.nextInt(); int roomNumber = read.nextInt(); Customer customer = new Customer(); //set customer's data to object here customer.saveCustomerInfo(); } } class Customer { //add all necessary attributes here String firstName; String secondName; int age; int roomNumber; public void saveCustomerInfo() { System.out.println("First name: " + firstName); System.out.println("Second name: " + secondName); System.out.println("Age: " + age); System.out.println("Room number: " + roomNumber); } }

4/29/2021 3:40:36 PM

Remus Tanasa

5 Answers

New Answer

+4

[Page 2] // continue... class Customer { //add all necessary attributes here String firstName; String secondName; int age; int roomNumber; // constructor Customer(String fn, String sn, int a, int rn){ this.firstName = fn; this.secondName = sn; this.age = a; this.roomNumber = rn; } public void saveCustomerInfo() { System.out.println("First name: " + firstName); System.out.println("Second name: " + secondName); System.out.println("Age: " + age); System.out.println("Room number: " + roomNumber); } }

+1

[Page 1] Your class attributes is not getting values. One way is by creating a constructor. This will help you assign values to your class attributes during object creation. INPUT: John Doe 30 123 [CODE] import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String firstName = read.nextLine(); String secondName = read.nextLine(); int age = read.nextInt(); int roomNumber = read.nextInt(); Customer customer = new Customer(firstName, secondName, age, roomNumber); //set customer's data to object here customer.saveCustomerInfo(); } }

0

i ve tried several times but it doesn't work. Basically, how to assign the object attribute to the input??

0

0

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String firstName = read.nextLine(); String secondName = read.nextLine(); int age = read.nextInt(); int roomNumber = read.nextInt(); Customer customer = new Customer(); //set customer's data to object here customer.setFirstName(firstName); customer.setSecondName(secondName); customer.setAge(age); customer.setRoomNumber(roomNumber); customer.saveCustomerInfo(); } } class Customer { //add all necessary attributes here String firstName; String secondName; int age; int roomNumber; public void setFirstName (String firstName){ this.firstName = firstName; } public void setSecondName (String secondName){ this.secondName = secondName; } public void setAge (int age){ this.age = age; } public void setRoomNumber (int roomNumber) { this.roomNumber = roomNumber; } public void saveCustomerInfo() { System.out.println("First name: " + firstName); System.out.println("Second name: " + secondName); System.out.println("Age: " + age); System.out.println("Room number: " + roomNumber); } }